2009-10-20 74 views
2

爲什麼在下面的例子中,組合框設置爲空而不是「先生」?如何設置XAML Combobox的選定值?

XAML:

<Window x:Class="TestComb82822.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 HorizontalAlignment="Left"> 
     <ComboBox SelectedValue="{Binding Salutation}" Width="200"> 
      <ComboBoxItem>Company</ComboBoxItem> 
      <ComboBoxItem>Ms.</ComboBoxItem> 
      <ComboBoxItem>Mr.</ComboBoxItem> 
     </ComboBox> 
    </StackPanel> 
</Window> 

後面的代碼:

using System.Windows; 
using System.ComponentModel; 

namespace TestComb82822 
{ 
    public partial class Window1 : Window, INotifyPropertyChanged 
    { 
     #region ViewModelProperty: Salutation 
     private string _salutation; 
     public string Salutation 
     { 
      get 
      { 
       return _salutation; 
      } 

      set 
      { 
       _salutation = value; 
       OnPropertyChanged("Salutation"); 
      } 
     } 
     #endregion 

     public Window1() 
     { 
      InitializeComponent(); 
      DataContext = this; 
      Salutation = "Mr."; 
     } 

     #region INotifiedProperty Block 
     public event PropertyChangedEventHandler PropertyChanged; 

     protected void OnPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 

      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
     #endregion 

    } 
} 

第二個嘗試:

布萊恩的SelectedItem和WindowLoaded也不管用,這仍使組合框空白:

XAML:

<Window x:Class="TestCombo234.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 HorizontalAlignment="Left"> 
     <ComboBox SelectedItem="{Binding Salutation}" Width="200"> 
      <ComboBoxItem>Company</ComboBoxItem> 
      <ComboBoxItem>Ms.</ComboBoxItem> 
      <ComboBoxItem>Mr.</ComboBoxItem> 
     </ComboBox> 
    </StackPanel> 
</Window> 

代碼背後:

using System.Windows; 
using System.ComponentModel; 

namespace TestCombo234 
{ 
    public partial class Window1 : Window, INotifyPropertyChanged 
    { 
     #region ViewModelProperty: Salutation 
     private string _salutation; 
     public string Salutation 
     { 
      get 
      { 
       return _salutation; 
      } 

      set 
      { 
       _salutation = value; 
       OnPropertyChanged("Salutation"); 
      } 
     } 
     #endregion 

     public Window1() 
     { 
      InitializeComponent(); 
      DataContext = this; 
      Loaded += new RoutedEventHandler(Window1_Loaded); 
     } 

     void Window1_Loaded(object sender, RoutedEventArgs e) 
     { 
      Salutation = "Mr."; 
     } 

     #region INotifiedProperty Block 
     public event PropertyChangedEventHandler PropertyChanged; 

     protected void OnPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 

      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
     #endregion 
    } 
} 

回答

0

默認情況下,ComboBox的確應該選擇它的第一項。但是,您在此處創建了SelectedValue的綁定,這是默認雙向。更重要的是,在負載情況下,Salutation(綁定到的)的值實際上仍然爲空。嘗試設置Salutation = "Mr.";之前InitializeComponent和所有應該沒問題。

+0

即使稱呼= 「先生」是在InitializeComponent之前,組合框仍然是空的:-( – 2009-10-20 13:10:58

+1

@愛德華:也許嘗試在XAML中嘗試綁定'SelectedItem'而不是'SelectedValue'? – Noldorin 2009-10-20 13:35:13

+0

謝謝但SelectedItem仍然使組合框空白 – 2009-10-20 13:47:37

1

首先,您需要設置SelectedItem而不是SelectedValue。其次,在實際設置ComboBox之前設置SelectedItem,嘗試在Window的加載事件中設置SelectedItem。

+0

我試過了,但組合框仍然是空的(上面的代碼) – 2009-10-20 14:39:30

+3

哦!你的項目不是字符串,你需要使用ItemsSource屬性以使用SelectedItem和/或SelectedValue。您也可以通過使用先生而不只是 Mr。 2009-10-20 16:39:18

1

我在這種情況下的解決方案是隻使用ItemIndex,即「先生」 = 2

0

你可以在後面的代碼中創建List/Observable集合。

public ObservableCollection<string> Collection 
     { 
      get; set; 
     } 

如:

<ComboBox Name="NameCombo" ItemsSource="{Binding}"> 

然後設置觀察集合作爲的datacontext在Window_Loaded方法組合框。

如:

NameCombo.DataContext=Collection; 
相關問題