0
votes

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?

2
If I understand correct then both ComboBoxes are same (even source is also same) and you want to set same value in both on selection on any one. Am I correct?Gaurang Dave
Well, if they are not the same procedure checks that and nothing happens, and that is implemented. I do not know hot to force it when they are the sameWojciech Szabowicz
What happens when First ComboBox has All selected. Is there equivalent value in the second combo box available? And to answer the question you could bind the SelectedItem of the first combo box to a property and in the setter of said property set the SelectedAxmModule, because Binding is TwoWay ComboBox will show updated value. Also you don't need SelectedValuePath on your Target ComboBox.XAMlMAX
Bear in mind that the value that you select for the Target ComboBox needs to be in the ItemsSource of said ComboBox. Have you tried setting the Target ComboBox IsEditable="true"? If this will show a newly selected item then it means that your object was NOT part of the collection of your ComboBox.XAMlMAX
It is a List<byte>Wojciech Szabowicz

2 Answers

1
votes

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.

0
votes

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.