WinRT development: In MVVM mode, winrtmvvm

Source: Internet
Author: User

WinRT development: In MVVM mode, winrtmvvm

The three binding techniques are described as follows:

  • Bind Command to MenuFlyout of ListViewItem in ListView;
  • Bind the selected project in the event of ListView, that is, its SelectedItem attribute;
  • The default value 0 in binding a text control to a numeric property is solved;

1. Bind Command to MenuFlyout of the list item in ListView;

When DataTemplate is compiled for ListView and MenuFlyout is added to it, and MenuFlyoutItem needs to bind the XXXCommand attribute in ViewModel. The code is similar to the following:

    <Page.Resources>        <DataTemplate x:Key="CategoryItemTemplate">            <Grid Holding="Grid_Holding">                <FlyoutBase.AttachedFlyout>                    <MenuFlyout>                        <MenuFlyoutItem x:Uid="MenuItem_Edit" Command="{Binding EditCategoryCommand}" CommandParameter="{Binding}"/>                        <MenuFlyoutItem x:Uid="MenuItem_Delete" Command="{Binding DeleteCategoryCommand}" CommandParameter="{Binding}"/>                    </MenuFlyout>                </FlyoutBase.AttachedFlyout>                <TextBlock Text="{Binding Name}" FontSize="24" Margin="0,5" />            </Grid>        </DataTemplate>    </Page.Resources>

At this time, if you run it, you will find that when you click MenuFlyoutItem, the corresponding method will not be triggered, that is, such binding is invalid. A recommended solution is, put VeiwModel as a resource in XAML, and then change the value of Command:

Command="{Binding EditCategoryCommand,Source={StaticResource viewModelName}}"

This can solve the problem, but there are two problems: 1. This viewModel is a newly created object, and it is not the same as the ViewModel object you actually use. 2, viewModel must have no parameter Constructor (in fact, this is associated with the first problem ).

After checking some information on the Internet, I finally found a perfect solution, as follows: Modify the Command value:

Command="{Binding DataContext.EditCategoryCommand,ElementName=pageCategory}"

Here, pageCategory is the Name value of the Page, so that MenuFlyoutItem can correctly respond to your operation. As for the value of the modified Command, I believe you will understand it at a glance and will not repeat it here.

 

2. Bind the selected project to the ListView event, that is, its SelectedItem attribute

First look at the code, pay attention to the bold part:

            <ListView x:Name="listCategory" ItemsSource="{Binding Categories}" ItemContainerStyle="{StaticResource StretchListViewItemStyle}">                <Interactivity:Interaction.Behaviors>                    <Core:EventTriggerBehavior EventName="SelectionChanged">                        <Core:InvokeCommandAction Command="{Binding CategorySelectedCommand}" CommandParameter="{Binding ElementName=listCategory,Path=SelectedItem}" />                    </Core:EventTriggerBehavior>                </Interactivity:Interaction.Behaviors>                ...

This is actually very simple. I wrote it down because I had taken a detour and didn't write a Path before. Then, in the method pointed to by Command, I transferred parameter to the ListView object, then obtain its SelectedItem attribute. Now it seems that it is a detour. The reason is that you are not familiar with the binding in WPF.

 

3. Handling the default value 0 in binding text controls to numeric attributes

When the Text attribute of the Text box is bound to a numeric attribute, if the numeric attribute is not assigned a value, its default value 0 is displayed in the Text box at runtime, if you do not want to display the default value 0, you can use a Converter to solve the problem. The Code is as follows:

    public class PriceCheckConverter : IValueConverter    {        public object Convert(object value, Type targetType, object parameter, string language)        {            float result = (float)value;            if (result <= 0)            {                return string.Empty;            }            else            {                return result.ToString();            }        }        public object ConvertBack(object value, Type targetType, object parameter, string language)        {            return value;        }    }

In binding, you can directly specify the Converter. The Code is as follows:

Text="{Binding Bill.Price,Mode=TwoWay,Converter={StaticResource PriceCheckConverter}}"

If you have a better method, please reply.

 

References:

Windows 8.1 Command Binding in a DataTemplate

How to implement a navigation button in shared application resources?

(The above two links are for the first problem)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.