ComboBox wpf没有选择项目

时间:2010-03-20 22:54:19

标签: wpf binding

我正在尝试将一个组合框绑定到一个对象列表,它的效果很好,除了选中的值之外,我还缺少某些东西吗?

<ComboBox ItemsSource="{Binding OrderInfoVm.AllCountries}"
          SelectedValuePath="country_code" DisplayMemberPath="country_name" 
          SelectedValue="{Binding OrderInfoVm.BillingCountry}" />

基本上我想将值绑定到国家/地区代码,并将所选值设置为绑定到OrderInfoVm.BillingCountry(实现INotifyPropertyChanged)的国家/地区代码

最初当控件加载选中的值为空时,但在点击BillingCountry时会填充。选定的值似乎没有变化。我该如何解决这个问题?

5 个答案:

答案 0 :(得分:2)

我同意Alex的观点,即使用SelectedItem会产生所需的行为。请参阅下面的代码。它有效并希望能够进一步帮助您:

    <Window x:Class="SelectedValueSpike.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <ComboBox ItemsSource="{Binding OrderInfoVm.AllCountries}" 
          SelectedValuePath="country_code" DisplayMemberPath="country_name"  
          SelectedItem="{Binding OrderInfoVm.BillingCountry}"
          IsSynchronizedWithCurrentItem="True"
          Name="AllCountriesBox"/>
        <TextBox Text="{Binding ElementName=AllCountriesBox, Path=SelectedValue}"/>
        <Button>
            Change the textbox to "Ca","NL",or "US" and click!
        </Button>
    </StackPanel>
</Window>

    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows;

    namespace SelectedValueSpike
    {
        public partial class Window1 : Window
        {
            public OrderInfoVm OrderInfoVm{ get; set;}
            public Window1()
            {
                InitializeComponent();
                OrderInfoVm=new OrderInfoVm();
                OrderInfoVm.AllCountries.Add(new Country("US","US of A"));
                OrderInfoVm.AllCountries.Add(new Country("NL","Netherlands"));
                OrderInfoVm.AllCountries.Add(new Country("Ca","Canada"));
                OrderInfoVm.BillingCountry = OrderInfoVm.AllCountries[1];
                DataContext = this;
            }
        }



public class OrderInfoVm:INotifyPropertyChanged
    {
        public OrderInfoVm()
        {
            AllCountries=new ObservableCollection<Country>();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private ObservableCollection<Country> _allCountries;
        public ObservableCollection<Country> AllCountries
        {
            get { return _allCountries; }
            set
            {
                _allCountries = value;
                OnPropertyChanged("AllCountries");
            }
        }

        private Country _billingCountry;
        public Country BillingCountry
        {
            get { return _billingCountry; }
            set
            {
                _billingCountry = value;
                OnPropertyChanged("BillingCountry");
            }
        }

        private void OnPropertyChanged(string property)
        {
            if(PropertyChanged!=null)
                PropertyChanged(this,new PropertyChangedEventArgs(property));
        }
    }

    public class Country
    {
        public string country_code { get; set; }
        public string country_name { get; set; }

        public Country(string code, string name)
        {
            country_code = code;
            country_name = name;
        }
    }
}

答案 1 :(得分:1)

也许您正在尝试实施与此类似的内容:Bound ComboBox

答案 2 :(得分:0)

尝试将其更改为SelectedItem并设置Mode = TwoWay ...

<ComboBox ItemsSource="{Binding OrderInfoVm.AllCountries}"
          SelectedValuePath="country_code" DisplayMemberPath="country_name" 
          SelectedItem="{Binding OrderInfoVm.BillingCountry, Mode=TwoWay}" />

编辑:您可能不需要将其更改为SelectedItem,也许只是设置TwoWay会起作用,但这就是我在自己的代码中完成它的方式。

答案 3 :(得分:0)

请确保您指定了正确的绑定路径。 尝试在调试模式下启动项目并查看输出窗口以查看是否存在任何绑定错误

答案 4 :(得分:0)

给它一个机会;我相信您已将SelectedValuePath和SelectedValue混淆:

<ComboBox ItemsSource="{Binding OrderInfoVm.AllCountries}"
   SelectedValue="country_code" 
   DisplayMemberPath="country_name" 
   SelectedValuePath="{Binding OrderInfoVm.BillingCountry}" />

供参考:

ItemsSource =获取或设置用于生成ItemsControl(ComboBox)内容的集合。

SelectedValue =获取或设置通过SelectedValuePath获得的SelectedItem的值。

SelectedValuePath =获取或设置一个值,该值指示从SelectedItem获取SelectedValue的路径。

DisplayMemberPath =获取或设置源对象上的值的路径,以用作对象的可视表示形式。

相关问题