(WPF) mvvm: ComboBox binding

Source: Internet
Author: User

The basic idea is to bind the viewmodel attribute in the view Xmal. Although binding can be implemented in the view background code, the relative amount of code in the Xmal is less.

In this example, a list <customer> is bound to a ComboBox, and the age of the selected customer is displayed in a textblock.

 

1. Model

    public class Customer    {        public string Name        {            get;            set;        }        public int Age        {            get;            set;        }    }

2. viewmodel

    public class CustomerViewModel : ViewModelBase    {        private List<Customer> customers;        private Customer selectedCustomer;         public CustomerViewModel()        {            this.customers = new List<Customer>()            {                new Customer { Name = "Paul", Age = 28 },                new Customer { Name = "Fred", Age = 37 },                new Customer { Name = "Cherry", Age = 21 },            };            this.selectedCustomer = new Customer();         }        public List<Customer> Customers        {            get            {                return this.customers;            }            set            {                if (!this.customers.Equals(value))                {                    this.customers = value;                    base.OnPropertyChanged("Customers");                 }            }        }        public Customer SelectedCustomer        {            get            {                return this.selectedCustomer;            }            set            {                if (!this.selectedCustomer.Equals(value))                {                    this.selectedCustomer = value;                    base.OnPropertyChanged("SelectedCustomer");                 }            }        }    }

3. View.

<UserControl x:Class="WpfApplication1.View.CustomerView"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"              xmlns:vm="clr-namespace:WpfApplication1.ViewModel"             mc:Ignorable="d"             Height="308.072"             Width="457.399">    <UserControl.DataContext>        <vm:CustomerViewModel/>    </UserControl.DataContext>    <Grid>        <ComboBox HorizontalAlignment="Left"                  Margin="45,47,0,0"                  VerticalAlignment="Top"                  Width="120"                  Height="31"                   ItemsSource="{Binding Customers}"                  SelectedItem="{Binding SelectedCustomer}"                  DisplayMemberPath="Name"/>        <TextBlock HorizontalAlignment="Left"                   Margin="212,52,0,0"                   TextWrapping="Wrap"                   Text="{Binding SelectedCustomer.Age}"                   VerticalAlignment="Top"                   Height="26"                   Width="101" />    </Grid></UserControl>

 

Other binding methods are as follows:

    <TextBlock Text="Example 1" VerticalAlignment="Center"/>            <ComboBox ItemsSource="{Binding MyStringOptions}" Grid.Column="1" SelectedItem="{Binding SelectedOption1}" Margin="5"/>            <TextBlock Text="{Binding SelectedOption1}" Grid.Column="2" Margin="10,5,5,0" VerticalAlignment="Center"/>            <TextBlock Grid.Row="1" Text="Example 2" VerticalAlignment="Center"/>            <ComboBox Grid.Row="1" Grid.Column="1" ItemsSource="{Binding MyClassOptions}" SelectedItem="{Binding SelectedClass}" DisplayMemberPath="Name" Margin="5"/>            <TextBlock Grid.Row="1" Grid.Column="2" Margin="10,5,5,0" VerticalAlignment="Center"><Run Text="{Binding SelectedClass.Name}"/><Run Text=" - "/><Run Text="{Binding SelectedClass.Age}"/></TextBlock>            <TextBlock Grid.Row="2" Text="Example 3" VerticalAlignment="Center"/>            <ComboBox Grid.Row="2" Grid.Column="1" ItemsSource="{Binding MyClassOptions}" SelectedValuePath="Age" SelectedValue="{Binding SelectedAge}" DisplayMemberPath="Name" Margin="5"/>            <TextBlock Grid.Row="2" Grid.Column="2" Margin="10,5,5,0" VerticalAlignment="Center"><Run Text="Selected age: "/><Run Text="{Binding SelectedAge}"/></TextBlock>            <TextBlock Grid.Row="3" Text="Example 4" VerticalAlignment="Center"/>            <ComboBox Grid.Row="3" Grid.Column="1" ItemsSource="{Binding MyClassOptions}" SelectedValuePath="Age" SelectedValue="{Binding SelectedAge}" ItemTemplate="{StaticResource Example4ItemTemplate}" Margin="5"/>            <TextBlock Grid.Row="3" Grid.Column="2" Margin="10,5,5,0" VerticalAlignment="Center"><Run Text="Selected age: "/><Run Text="{Binding SelectedAge}"/></TextBlock>

 

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.