Mit folgender SmartComboBox kann man direkt im XAML unter Verwendung des ObjectDataProvider x-beliebige Parameter angeben, welche dann als ComboBoxItems zur Verfügung stehen. Gespeichert werden die Items als KeyValuePair<int, string>.
BasisControl:
SmartComboBox XAML
1 2 3 4 5 6 7 8 | <ComboBox x:Class="Core.Controls.SmartComboBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Value}" /> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> |
SmartComboBox CodeBehind
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public partial class SmartComboBox : ComboBox { /// <summary> /// Initializes a new instance of the <see cref="SmartComboBox"/> class. /// </summary> public SmartComboBox() { InitializeComponent(); SelectedValuePath = "Key"; } /// <summary> /// Gets the smart combobox items. /// </summary> /// <param name="args">The args.</param> /// <returns></returns> public ItemCollection GetSmartComboboxItems(params string[] args) { if (args.Length < 0) return null; Items.Clear(); for (int i = 0; i < args.Length; i++) { Items.Add(new KeyValuePair<int, string>(i, args[i])); } return Items; } } |
Verwendung der SmartComboBox
Namespace erweitern:
1 2 | xmlns:Controls="clr-namespace:Core.Controls;assembly=Core" xmlns:System="clr-namespace:System;assembly=mscorlib" |
Den ObjectDataProvider in (Windows, Usercontrol).Resources definieren und die gewünschten Items angeben.
1 2 3 4 5 6 7 8 9 | <ObjectDataProvider x:Key="GetSmartComboboxValues" ObjectType="{x:Type Controls:SmartComboBox}" MethodName="GetSmartComboboxItems"> <ObjectDataProvider.MethodParameters> <System:String>Bitte wählen</System:String> <System:String>Unwichtig</System:String> <System:String>Wichtig</System:String> <System:String>Sehr wichtig</System:String> <System:String>Eilt</System:String> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> |
Und verwenden an gewünschter Stelle wie folgt:
1 2 | <Controls:SmartComboBox ItemsSource="{Binding Source={StaticResource GetSmartComboboxValues}}" SelectedValue="{Binding Wichtigkeit, UpdateSourceTrigger=LostFocus}" /> |
Viel Spass beim entwickeln : )







