How to do Two Way binding within two combo boxes in Silverlight - c#

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" />

Related

How to Binding ComboBox with TextBox?

I have a class named Order with properties of ID and Name.
I have an ObservableCollection<Order> named AllOrder.
I have a ComboBox
<ComboBox Name="CID" DataContext="{Binding ElementName=MainScreen}"
ItemsSource="{Binding AllOrder}" DisplayMemberPath="ID"/>
When I select the ComboBox I want TextBox to display Name, something like:
<TextBox Text="{Binding ElementName=CID,Path=SelectedItem,Display=Name}">
You can bind to nested properties:
<TextBox Text="{Binding ElementName=CID, Path=SelectedItem.Name}" />
SelectedItem returns the currently selected Order object and .Name returns the value of the Name property of this one.

WPF change selected value in one ComboBox by another ComboBox in MVVM

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.

Binding WPF combobox to a user settings property

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()

Bound text of a TextBox different to bound "item" possible?

XAML
<TextBox Text="{Binding SelectedKey.Name}" />
I want to bind an instance of SelectedKey to a TextBox, but show the property SelectedKey.Name as text.
How is this possible? I could not find a TextBox property like "DataBoundItem".
If the DataContext of the TextBlock has a property called "SelectedItem" that returns an object with a SelectedKey property you could bind to the Name property of this latter one like this:
<TextBox Text="{Binding SelectedItem.SelectedKey.Name}" />
If you want to bind to the SelectedKey property of an item that is currently selected in an ItemsControl such as for example a ListBox, you could use an ElementName binding:
<ListBox x:Name="listBox1" ... />
<TextBox Text="{Binding SelectedItem.SelectedKey.Name, ElementName=listBox1}" />
If you need any further help you should read this:
How to create a Minimal, Complete, and Verifiable example
I could not find a TextBox property like "DataBoundItem".
A TextBox doesn't have any "DataBoundItem" property. It has a Text property for displaying the text in the TextBox and that's it basically.
All controls that inherit from FrameworkElement has a Tag property of type object that you can bind anything you want to though. You can use this one:
<TextBox Text="{Binding SelectedKey.Name}" Tag="{Binding SelectedKey}" />

Cannot set SelectedIndex to 0

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

Categories

Resources