2010-03-20 50 views
0

我想将一个组合框绑定到一个对象列表,并且除了选定的值以外,它工作得很好,我是否缺少某些内容?组合框wpf不是项目未被选中

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

基本上我要绑定到值国家代码,并设置所选择的值绑定到OrderInfoVm.BillingCountry(其实现INotifyPropertyChanged)国家代码

当所选择的控制负载值是空的最初,但点击BillingCountry填充。选定的值似乎没有改变。我该如何补救?

+0

'的SelectedValue =“{结合OrderInfoVm.BillingCountry.country_code}”的作品有所好转过的我在回答中给出的代码。但是,在这种情况下,文本框不会与组合框同步。使用SelectedItem! – Dabblernl 2010-03-21 01:54:55

回答

2

我同意亚历克斯使用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; 
     } 
    } 
} 
+0

非常感谢,所以这里发生了什么,我绑定到一个自定义这个类是我从数据库中填充的,无论我尝试什么,它都不会选择,当我将内容复制到字典后,所有东西都开始工作了,我不确定为什么会这样。我认为这是因为选定的值是一个字符串,而对象的值是SqlString类型。这可能吗? – 2010-03-22 14:55:12

+0

这是非常可能的。尽管如此,您应该有一个Databinding错误。您知道如何在输出窗口中获得详细的调试报告吗? http://bea.stollnitz.com/blog/?p=52 – Dabblernl 2010-03-22 15:46:11

0

试着改变它的SelectedItem和设置模式=双向...

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

编辑:您可能不需要将其更改为的SelectedItem,或许只是设置双向都可以工作,但是这是我怎么样了在我自己的代码中完成它。

+0

由于某种原因,既没有工作。此外,如果我将选定值更改为“美国”,它仍然不起作用:(我不明白 – 2010-03-20 23:13:12

+0

SelectedItem将无法使用SelectedValuePath,因为它选择对象本身 但是,如果值需要SelectedValue应该是 – Oleg 2010-03-20 23:32:30

0

请确保您指定了正确的绑定路径。在调试模式下 开始尝试的项目,并期待在输出窗口,看是否有任何绑定错误

+0

所有绑定路径是正确的:( – 2010-03-21 03:18:50

0

给这个一杆;我相信你有你的SelectedValuePath和的SelectedValue混合起来:

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

相关信息:

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

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

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

DisplayMemberPath =获取或设置一个路径到源对象上的值以作为所述对象的所述视觉表示。

+0

没有其实这是不正确的在我的情况下,selectedvaluepath和selectedvalue应该交换,我测试它两种方式 – 2010-03-22 14:52:03

1

也许你正在试图实现类似于此:Bound ComboBox

+0

谢谢,不完全是,我只是绑定到数据库对象,但这有点帮助:) – 2010-03-22 14:52:34