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
Related
I am having a problem. I have two combobox in WPF first:
<ComboBox commands:PropertyChangeBehavior.Command="{Binding GetCurrentsModuleCommand}" SelectedIndex="0">
<ComboBox.ItemsSource>
<CompositeCollection>
<ComboBoxItem IsEnabled="True">All</ComboBoxItem>
<CollectionContainer Collection="{Binding Source={StaticResource ResourceKey=AxmModulesCombo}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
</StackPanel>
Lets call it Source and it is binded to command:
public ICommand GetCurrentsModuleCommand => new Command(module =>
{
SelectedAxmModule = module.ToString();
Stuff(module);
});
And this combo box has no SelectItem property (well it don't needs any as parameters are only pass to method).
And a Target CombxBox
<ComboBox ItemsSource="{Binding AxmModules}"
SelectedItem="{Binding SelectedAxmModule, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectedValuePath="{Binding SelectedAxmModules}"
IsSynchronizedWithCurrentItem="True"/>
Witch SelectedItem property is binded to:
private string selectedAxmModule;
public string SelectedAxmModule
{
get => selectedAxmModule;
set
{
selectedAxmModule = value;
OnPropertyChanged();
}
}
Now those combo boxes are similar in values, now I want to do if I click value on source as you see in source command i want to select same value from source (I handled values from source and not in target in code so that is irrelevant).
I tried adding Update property and SelectedValuePath but no good value in target combo box is still empty after selecting value from source, and OnPropertyChange works as inteded. Is there a way to achieve this?
If AxmModules is an IEnumerable<byte>, SelectedAxmModule should be a byte property. You cannot set a string property to a byte value. The types must match.
And SelectedValuePath is superfluous when using SelectedItem.
Since you have
IsSynchronizedWithCurrentItem="True"/>
Then the selecteditem should be the current. You should work with the current item of the collectionview rather than binding selecteditem.
You can read more, probably more than you want to read, about collectionview and this sort of stuff here:
https://social.technet.microsoft.com/wiki/contents/articles/26673.wpf-collectionview-tips.aspx
Also.
See this
SelectedValuePath="{Binding SelectedAxmModules}"
The selectedvaluepath is a string not a binding. It should be the name of whichever field you want to use.
If, for whatever reason, you want to bind to the current item in a collection then you can use the / notation.
https://social.technet.microsoft.com/wiki/contents/articles/29859.wpf-tips-bind-to-current-item-of-collection.aspx
I'm not sure how this is going to fit with a composite collection though. I prefer a separate control to indicate selection of "all" and hence wouldn't use a composite collection in this way. You might want to consider that approach yourself.
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'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 have a problem with my ComboBox in WPF/MVVM project. After changing ViewModel (I use everything correctly: INotifyPropertyChanged, ObservableCollection, etc.) the ComboBox is not always changes its value. I've debugged it and the getter from ViewModel returned the correct value (string.Empty), but the ComboBox displayed a value from the list. A bit of code below:
// It doesn't work.
<ComboBox ItemsSource="{Binding ItemsCollection}" SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
// It works.
<TextBlock Text="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
TextBlock is updated correctly if SelectedItem is string.Empty or null, but ComboBox is not updated correctly (old value stays) if SelectedItem is string.Empty. It's cleared only if SelectedItem is null value.
Why is this occurring?
// EDIT:
Maybe I explained it wrong. I have two grids: on the left I have tree with files and on the right I have properties of selected file. When I first select file A, which doesn't have setted property, the ComboBox is empty. If I choose file B, which have this property setted, it appers in the ComboBox. Next if I select file C with empty property, the old value stays in ComboBox (from before selected file - B). If this property from C is null, not string.Empty, ComboBox is cleared.
Use CollectionViewSource instead of ObservableCollection for ComboBox.
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" />