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.
Related
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()
I have this element in my xaml (DevExpress item):
<dxe:ComboBoxEdit IsTextEditable="False" EditValue="{Binding IDTIPOCONN}"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window},Path=DataContext.ttc}" />
All the binding are correct, and when I change the value, I trigger some event from my ViewModel.
My problem is the update of the value is executed only when I leave the focus of my comboBox. I need, instead, to execute my action when the value is changed, before leaving its foucs.
How can I do it? I need this because selecting one or another from the list, I show to the user some hidden elements.
Try for Binding set UpdateSourceTrigger=PropertyChanged:
<dxe:ComboBoxEdit IsTextEditable="False"
EditValue="{Binding Path=IDTIPOCONN, UpdateSourceTrigger=PropertyChanged}" ... />
In this case, most likely the default value of UpdateSourceTrigger is LostFocus.
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" />
I have a window control in which I am loading a wpf user control. The wpf user control contains a combo box which is bound to a property in the view model. When I am debugging the code selected value property always has a value. When the control is loaded for the first time I am getting the selected value, but when I reload the control the combo box gets cleared. I tried everything; updating the source and other stuff, but it's still not working. I even set the datacontext in code behind, but am still not getting the expected output.
This is the XAML code:
<ComboBox x:Name="cmbType" Style="{StaticResource ComboBoxStyle}"
PreviewTextInput="cmbErrorType_PreviewTextInput" IsEditable="True"
MaxDropDownHeight="100" ItemsSource="{Binding Path=TypeList,Mode=OneWay}"
SelectedValue="{Binding Path=SelectedValue}" DisplayMemberPath="Id"
SelectedValuePath="Id" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2"/>
When trying to set a ComboBox SelectedIndex to 0 so I have the first value as default, doesn't work, the combobox its empty by default. If I try setting it to any other number it works, I have the selected index as a default value.
Anyone have an idea why I can't set it to 0 from xaml ?
<RSControls:SmoothScrollComboBox
Grid.Column="1"
x:Name="comboTypes"
Margin="7,0,0,0"
SelectedValue="{Binding Path=SelectedTypes}"
SelectedValuePath="Name"
SelectedIndex="0"
ItemsSource="{Binding Source={StaticResource GroupedTypes}}"
DisplayMemberPath="Display"
SelectionChanged="comboTypes_SelectionChanged"
IsSynchronizedWithCurrentItem="True">
PS: forgot to mention that I have 2 comboboxes, the other one works, implemented in the same way, only the Selectedvalue and ItemSource differs.
It's probably because you are setting the selected item twice: once with SelectedIndex and once with SelectedValue
Remove the SelectedValue property and SelectedIndex should work