I've a combobox in WPF with 4 static values in it:
<ComboBox
SelectedValue="{Binding Source={x:Static properties:Settings.Default},
Path=KeyModifier, Mode=TwoWay}">
<ComboBoxItem>Alt</ComboBoxItem>
<ComboBoxItem>Shift</ComboBoxItem>
<ComboBoxItem>Ctrl</ComboBoxItem>
<ComboBoxItem>Win</ComboBoxItem>
</ComboBox>
I want to connect the selected value of this combobox with a simple string property in the user settings. That works half way: The selected value is perfectly written to Settings.Default.KeyModifier ... But after restarting the application the selected value of the combobox is not set ... despite that all other controls (Edits, Checkboxes) binded the same way on other properties are set correctly.
Is there some mystery on filling a combobox with values from a binded property?
Or do I have to do the whole selection process on startup manually in code behind?
Since you don't add strings, but ComboBoxItems to your ComboBox, you would also have to set its SelectedValuePath property:
<ComboBox SelectedValuePath="Content"
SelectedValue="{Binding Source={x:Static properties:Settings.Default},
Path=KeyModifier, Mode=TwoWay}">
<ComboBoxItem>Alt</ComboBoxItem>
<ComboBoxItem>Shift</ComboBoxItem>
<ComboBoxItem>Ctrl</ComboBoxItem>
<ComboBoxItem>Win</ComboBoxItem>
</ComboBox>
Alternatively add strings to the ComboBox, and use SelectedItem instead of SelectedValue:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
...
<ComboBox SelectedItem="{Binding Source={x:Static properties:Settings.Default},
Path=KeyModifier, Mode=TwoWay}">
<sys:String>Alt</sys:String>
<sys:String>Shift</sys:String>
<sys:String>Ctrl</sys:String>
<sys:String>Win</sys:String>
</ComboBox>
Note also that since WPF 4.5 you may write the Binding like this:
SelectedItem="{Binding Path=(properties:Settings.Default).KeyModifier, Mode=TwoWay}"
Have you saved the settings after you change the values? Settings.Default.Save()
Related
So I searched in this forum but couldn't find the anwser...
I'm Binding my combobox to a list of users. But when I want to type in it for example a name, some wierd stuff is comming out.
XAML Code:
<ComboBox Name="UserLoginCmbBox" Width="100" IsEditable="True" SelectionChanged="UserLoginCmbBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Login}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Code Behind Code:
var users = new List<User>{...} //initialization
UserLoginCmbBox.ItemsSource = users;
When I'm typing in the box only string I can get is:
System.Data.Entity.DynamicProxies.User_FE59351B6479342209B496E7D3C3B877DDC51FE15279FFFEF899A5012F85FCBA
I found out that this is what ToString() method returns, but what if I want to use the same list for expamle in another ComboBox where I want to choose the Name or something else?
How can I modify SelectionItemBox? Cause when I tried to use the SelectionItemBoxTemplate to write my own template, an error occured (SelectionItemBoxTemplate has only getter).
Try to set the DisplayMemberPath property to "Login":
<ComboBox Name="UserLoginCmbBox" Width="100" IsEditable="True" SelectionChanged="UserLoginCmbBox_SelectionChanged" DisplayMemberPath="Login" />
I'm using Bindings to set the ItemsSource of a combobox.
How can I set the index of my combobox to 0 without an event handler or will I have to use an event handler?
SelectedIndex in WPF gets ignored since SelectedValue is already assigned to a binding.
<ComboBox SelectedValue="{Binding Class}"
ItemsSource="{Binding Source={StaticResource classList}}"
SelectedIndex="0"/>
<ComboBox SelectedValue="{Binding ItemSubClass}"
ItemsSource="{Binding Class.AvailableSubClasses}"
SelectedIndex="0"/>
Selected value on cb2 is null when cb1.value changes. And cb1.value is null on load.
I'm working in a C# project [for school], using WPF and implementing MVP. In this code, I've got a DataGrid showing a list of divers. The first column is the Name, and the second column shall show 'DivingType'. DivingType is a built in object, which has a property ID, such as 103A. There are about 400 of these, stored in a list, and each Diver ('row') has a Dives (List<Dive>) Property, and each of these Dives has a divingType property.
What we want to have, is that this column will by default show the DivingType.ID associated with the diver, but that the dropdown list shall contain ALL diving types, such that you shall be able to change it from there [and update the diver object]. To further complicate it, this is one of many views which we add to our window as UserControls.
With that said, here is the code. I've tried to cut out unnecessary clutter which I'm certain has no impact on the result.
<Window ...>
<Window.Resources>
<local:Presenter x:Key="myPresenter"/>
</Window.Resources>
<DockPanel DataContext="{StaticResource myPresenter}">
<UserControl ...>
<DataGrid ItemsSource="{Binding DiverList}" x:Name="datagrid">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="1*" Binding="{Binding Name}"/>
<DataGridTemplateColumn Header="Diving type" Width="1*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl},
Path=DataContext.DivingTypes}"
DisplayMemberPath="ID"
SelectedValue="{Binding Dives[0]}"
SelectedValuePath="divingType">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</UserControl>
</DockPanel>
</Window>
When the program runs I get all DivingTypes.ID inside the combobox, but no selected value. The code does not put any related errors into the output window. I believe that what happens is that it calls DivingType.Equals but passing the DataContext for the row (the Diver) instead of the SelectedValuePath which I specify. Any way to override this behaviour inside XAML? Or is there an easier way to achieve this?
EDIT:
I've since edited the code posted above to be:
<ComboBox ItemsSource="{
Binding RelativeSource={
RelativeSource AncestorType=UserControl
},
Path=DataContext.DivingTypes
}"
SelectedValue="{
Binding Dives[0].divingType, Mode=TwoWay
}"
/>
This makes the correct value show in the combobox at the start, DivingType.ID is loaded from the Diver.Dives[0].divingType, but it still does not set the property when I select a new value in the dropdown box.
Use UpdateSourceTrigger=PropertyChanged.
Explanation here.
Have you tried to implemented INotifyPropertyChanged in your viewmodel and then raise the PropertyChanged event when the SelectedValue gets set.
If this is not working, can you set the SelectedValue
SelectedValue="{Binding Path=divingType, Mode=TwoWay}"
I have the following situation: inside xaml I get values from a database and fill the combobox..if the selected item in the combobox has a value "x" I want to hide some elements from the working window..thx for your tips
<TextBlock Text="XYZ:"/>
<ComboBox ItemsSource="{Binding DataContext.KeyLists.XYZ,
RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
SelectedValuePath="XYZId"
SelectedValue="{Binding XYZId, Mode=TwoWay}"
DisplayMemberPath="Name" />
There are many ways to solve this.
you can make IValueConverter to convert selection values to Visibility, apply to each control with different converter parameter
you can write styles with triggers for the convtols
you could (not recommended) handle this in code
In a project I am showing two ComboBoxes lets say ComboBox1 and ComboBox2. I am binding both the ComboBox with a KeyValue pair Dictionary lets say dictionary1 as given below.
ComboBox1.ItemsSource = dictionary1 ;
ComboBox1.SelectedItem = ComboBox1.Items[0];
//Setting the Item Source of Patient Name Combo Box.
ComboBox2.ItemsSource = dictionary1 ;
ComboBox2.SelectedItem = ComboBox2.Items[0];
and in the XAML part, I am showing Key in the CombBox1 and Value in the ComboBox2 of the Dictionary as mentioned below:-
<ComboBox
x:Name ="ComboBox1"
DisplayMemberPath ="Key"
SelectedValue ="{Binding Source=ComboBox2, Path=DisplayMemberPath, Mode=TwoWay}"/>
<ComboBox
x:Name ="ComboBox2"
DisplayMemberPath ="Value"
SelectionChanged ="ComboBox2_SelectionChanged"
/>
Objective:-
If I change the selection in ComboBox1 then it should affect the corresponding value of the ComboBox2.SelectedItem and also if I change the selection in ComboBox2 then it should affect the corresponding key value in the ComboBox1.SelectedItem.
Can anybody tell me where is the mistake in my above code or please help me in accomplish the above said objective. Thanks in advance.
This should work
<ComboBox
x:Name ="ComboBox1"
DisplayMemberPath ="Key"
SelectedItem ="{Binding ElementName=ComboBox2, Path=SelectedItem, Mode=TwoWay}"/>
<ComboBox
x:Name ="ComboBox2"
DisplayMemberPath ="Value"
/>
I can see at least two problems:
Needs to be ElementName instead of Source
Should be Path instead of DisplayMemberPath
I think this should work:
<ComboBox
x:Name ="ComboBox1"
DisplayMemberPath ="Key"
SelectedValue ="{Binding ElementName=ComboBox2, Path=SelectedValue}"/>
<ComboBox
x:Name ="ComboBox2"
DisplayMemberPath ="Value" />