2012-01-18 51 views
1

我有這個類如何通過自定義類後面的代碼填充WPF Combobox?

public class UILanguagesModel : INotifyPropertyChanged 
    { 
     public UILanguagesModel() 
     { 
      IList<UILanguage> list = new List<UILanguage>(); 

      UILanguage english = new UILanguage(); 
      english.Culture = "en"; 
      english.SpecCulture = "en-US"; 
      english.EnglishName = "English";   

      UILanguage spanish = new UILanguage(); 
      spanish.Culture = "es"; 
      spanish.SpecCulture = "es-ES"; 
      spanish.EnglishName = "Spanish"; 

      list.Add(english); 
      list.Add(spanish); 
      _languages = new CollectionView(list); 
     } 
     private readonly CollectionView _languages; 
     private UILanguage _language; 

     public CollectionView Languages 
     { 
      get { return _languages; } 
     } 

     public UILanguage Language 
     { 
      get { return _language; } 
      set 
      { 
       if (_language == value) return; 
       _language = value; 
       OnPropertyChanged("Language"); 
      } 
     } 
     private void OnPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
     public event PropertyChangedEventHandler PropertyChanged; 
    } 

    public sealed class UILanguage 
    { 
     public string EnglishName { set; get; } 

     public string Culture { set; get; } 

     public string SpecCulture { set; get; } 
    } 

我需要用 「EnglisgName」 WPF組合框來填充。

怎麼辦?

謝謝!


標記XAML

<ComboBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="0,1,0,0" 
        Name="cmbLanguages" VerticalAlignment="Top" Width="207" /> 
+0

你能顯示標記嗎? –

+0

@ShoaibShaikh是這裏。 –

+1

你爲什麼要以這種方式爲你的UI做多種語言支持?它有可能檢測出當前的系統文化。我討厭讓我選擇一種我甚至無法閱讀的語言的程序。 –

回答

3

當綁定一個ComboBox項目的集合,你通常會定義你的集合類作爲一個ObservableCollection:

public class UILanguages : ObservableCollection<UILanguage> 
{ 
} 

並將您的ComboBox綁定到使用ObservableCollection作爲源的CollectionViewSource,如在以下XAML中聲明的。 CollectonViewSource保存所選項目的軌跡。

<Window x:Class="ComboBoxTest.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:ComboBoxTest" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <local:UILanguages x:Key="UILanguages"/> 
     <CollectionViewSource x:Key="UILanguagesViewSource" Source="{StaticResource UILanguages}"/> 
    </Window.Resources> 
    <Grid> 
     <ComboBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="0,1,0,0" 
        Name="cmbLanguages" VerticalAlignment="Top" Width="207" 
        ItemsSource="{Binding Source={StaticResource UILanguagesViewSource}}"/> 
    </Grid> 
</Window> 

然後填充集合:

UILanguages languages = (UILanguages)Resources["UILanguages"]; 

languages.Add(
    new UILanguage 
    { 
     Culture = "en", 
     SpecCulture = "en-US", 
     EnglishName = "English" 
    }); 

languages.Add(
    new UILanguage 
    { 
     Culture = "es", 
     SpecCulture = "es-ES", 
     EnglishName = "Spanish" 
    }); 

當然,也可以定義的ObservableCollection和CollectionViewSource的代碼,這樣就避免了XAML資源聲明:

UILanguages languages = new UILanguages(); 

languages.Add(
    new UILanguage 
    { 
     Culture = "en", 
     SpecCulture = "en-US", 
     EnglishName = "English" 
    }); 

languages.Add(
    new UILanguage 
    { 
     Culture = "es", 
     SpecCulture = "es-ES", 
     EnglishName = "Spanish" 
    }); 

CollectionViewSource cvs = new CollectionViewSource 
{ 
    Source = languages 
}; 

cmbLanguages.SetBinding(ItemsControl.ItemsSourceProperty, new Binding { Source = cvs }); 

你也可能想要覆蓋您的UILanguage類中的ToString以顯示一些有用的內容:

public sealed class UILanguage 
{ 
    public string EnglishName { set; get; } 

    public string Culture { set; get; } 

    public string SpecCulture { set; get; } 

    public override string ToString() 
    { 
     return EnglishName; 
    } 
} 
+0

謝謝你的幫助!我使用你的代碼片斷了一些錯誤。錯誤「本地」是未聲明的前綴。第8行,第10位。 XML無效。 \t D:\ .... –

+0

我已經完成了XAML。本地只是項目名稱空間的XAML名稱空間聲明,ComboBoxTest在這裏。 – Clemens

+0

非常感謝!您的解決方案對我來說完全完美它工作正常! –

1

如果在View(與XAML相關的C#文件)的構造函數,你設置:

this.DataContext = new UILanguagesModel(); 

然後,它是有約束力的那樣簡單:

<ComboBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="0,1,0,0" 
        Name="cmbLanguages" VerticalAlignment="Top" Width="207" 
        ItemsSource="{Binding Languages}"/> 

然後你可能會想你的ComboBox的設定值在你的Language財產,在這種情況下,結合變爲:

<ComboBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="0,1,0,0" 
         Name="cmbLanguages" VerticalAlignment="Top" Width="207" 
         ItemsSource="{Binding Languages}" 
         SelectedValue="{Binding Language}"/> 

編輯:你可能不得不宣佈你Languages收藏作爲ObservableCollection

+0

對不起,我做你說的,但它不填充組合框。 –

+0

你可以發佈你的XAML的代碼隱藏嗎? –

+0

我用你的代碼。 –

相關問題