Simple ComboBox frage in WPF
-
Hallo Leute,
ich habe eine ComboBox, welches 2 "statische" Item enhält!
<ComboBox Text="Choose Type" SelectedItem="{Binding Path=SelObject }" Grid.Column="3" > <ComboBoxItem Name="CombineProcess">CombineProcess</ComboBoxItem> <ComboBoxItem Name="Alternative">Alternative</ComboBoxItem> </ComboBox>
ich hab dann die Auswahl an meine ViewModel Probperty SelObject gebunden (String)!
Das funktioniert auch, aber ich will jetzt nich das SelectedItem-Objet der Combobox gebunden ahben, sonder nur en inhalt des Combobox Items ,also "CombineProcess"oder "Alternative" haen, stattdessen bekomme ich "System.Windows.Controls.ComboBoxItem: CombineProcess" oder "System.Windows.Controls.ComboBoxItem: Alternative"?Grüße
-
Am besten noch wäre es, wenn ich ein enum binden könnte:
enum ProcessBehavior { Alternativ, Combine }
??
-
-
Kleiner Auszug meines Codes.
Ich habe es so gelöst.<ComboBox Height="23" HorizontalAlignment="Left" Margin="132,22,0,0" Name="featureGrp" VerticalAlignment="Top" Width="300"> <ComboBoxItem Name="GOR">Ziele, Vorhaben und Anforderungen</ComboBoxItem> <ComboBoxItem Name="ANA">Analysen</ComboBoxItem> <ComboBoxItem Name="SOL">Lösungen</ComboBoxItem>
public partial class FeatureDefinition : Window { public FeatureDefinition(GUI_data gui_data) { InitializeComponent(); this.DataContext = new ViewModel_GUI_data(gui_data); } }
das ich ViewModel so abfrage:
private string featureGrp; public string FeatureGrp { get { return featureGrp; } set { if (value != featureGrp) { featureGrp = value; RaisePropertyChanged("FeatureGrp"); } } }
und die Combo im Code an anderer Stelle so ansteuere:
private static GUI_data gui_data = new GUI_data(); ..... gui_data.FeatureType = "Orginaltext"; ..... var view = new FeatureDefinition(gui_data); ...... /* build up dynamik form settings*/ view.featureGrp.SelectedIndex = 0; view.FeatureGrp.Items.Add("Orginaltext"); .... view.ShowDialog();
-
Warum nutzt du nicht den ObjectDataProvider? Ungefähr so (ungetestet):
<Window ...> <Window.Resources> <ObjectDataProvider x:Key="MyEnumProvider" MethodName="GetValues" ObjectType="{x:Type System:Enum}"> <ObjectDataProvider.MethodParameters> <x:Type Type="local:MyEnum"/> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> </Window.Resources> <ComboBox ItemsSource="{Binding Source={StaticResource MyEnumProvider}}" SelectedItem="{Binding MyEnumValue, Mode=TwoWay}" /> </Window>
MyEnumValue ist natürlich eine Property in deinem ViewModel, die vom Typ MyEnum ist.
-
Ja, da gibt es aber ein kleines Problem bei.
INotifyPropertyChanged reagiert nicht auf default settings, sondern eben nur auf changed.
-
Zur ursprünglichen Frage im Eröffnungsbeitrag:
ComboBox.SelectedValue
-
Danke Freunde:)