(WPF) ComboBox 之绑定

1.  在UI(Xaml) 里面直接绑定数据.

<Window x:Class="WpfTutorialSamples.ComboBox_control.ComboBoxSample"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="ComboBoxSample" Height="150" Width="200">

    <StackPanel Margin="10">

        <ComboBox>

            <ComboBoxItem>ComboBox Item #1</ComboBoxItem>

            <ComboBoxItem IsSelected="True">ComboBox Item #2</ComboBoxItem>

            <ComboBoxItem>ComboBox Item #3</ComboBoxItem>

        </ComboBox>

    </StackPanel>

</Window>

效果如下:

A simple ComboBox control

 

2. 动态绑定数据.

2.1 绑定XML到ComboBox

     前台Xaml里面需要在Resource里面指定XmlDataProvider. 并设定绑定.  后台无需任何代码。

<Window x:Class="WpfApplication1.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="350" Width="525">

  <Window.Resources>

    <XmlDataProvider x:Key="DataProvider" Source="pack://siteoforigin:,,,/TestData.xml" XPath="Countries"/>

  </Window.Resources>

  <StackPanel>

    <ComboBox x:Name="com_Country" ItemsSource="{Binding Source={StaticResource DataProvider}, XPath=Country}" DisplayMemberPath="@Name" Margin="5"/>

    <ComboBox x:Name="com_City" DataContext="{Binding ElementName=com_Country, Path=SelectedItem}" ItemsSource="{Binding XPath=City}" DisplayMemberPath="@Name" Margin="5"/>

    <TextBlock Margin="5">

      <TextBlock.Inlines>

        <Run Text="Selected Country: "/>

        <Run Text="{Binding ElementName=com_Country, Path=SelectedItem.Attributes[Name].Value, Mode=OneWay}"/>

        <LineBreak/>        

        <Run Text="Selected City: "/>

        <Run Text="{Binding ElementName=com_City, Path=SelectedItem.Attributes[Name].Value}"/>

      </TextBlock.Inlines>

    </TextBlock>

  </StackPanel>

</Window>

 

3. 绑定Dictionary到ComboBox.

 

 private void BindingToProjects()

        {

            this.projects.Add("AAA", new List<string>()

            {

                "VID_045", 

                "VID_046"



            this.projects.Add("BBB", new List<string>()

            {

                "VID_111", 

                "VID_222", 

            });



            this.cbProjectName.ItemsSource = this.projects;

            this.cbProjectName.DisplayMemberPath = "Key";

            this.cbProjectName.SelectedValuePath = "Key"; 



        }



        private void cbProjectName_SelectionChanged(object sender, SelectionChangedEventArgs e)

        {

            this.lbVidPid.Items.Clear(); 



            List<string> registryList = this.projects.Where(dic => dic.Key == this.cbProjectName.SelectedValue.ToString()).Select(dic => dic.Value).First();   



            foreach (var registryKey in registryList)

            {

                this.usbRegistryList.Add(registryKey);

                this.lbVidPid.Items.Add(registryKey);

            }



        }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

参考: http://www.wpf-tutorial.com/list-controls/combobox-control/

你可能感兴趣的:(combobox)