2010-03-20 32 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