This is a follow-up of my previous question on a ComboBox in a C# WPF solution.

Background: I have two classes: Pallet and CUnit. I have a list of Pallet objects, called ActivePallets and a single CUnit, called Unit.

I have the following source (in a XAML):

<ComboBox x:Name="Cmb_MPNr" 
          ItemsSource="{Binding ActivePallets}"
          SelectedValue="{Binding Unit.MPNr, Mode=TwoWay}"
          SelectedValuePath="MPNr"
          IsTextSearchEnabled="True"
          TextSearch.TextPath="MPNr"
          IsEditable="True">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding MPNr}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

<TextBox Name="PO" Grid.Row="0" Grid.Column="2" IsEnabled="False">
    <TextBox.Text>
        <MultiBinding StringFormat="{}{0} - {1}">
            <Binding Path="SelectedItem.PO" ElementName="Cmb_MPNr" />
            <Binding Path="SelectedItem.Description" ElementName = "Cmb_MPNr" />
        </MultiBinding>
    </TextBox.Text>
</TextBox>

I understand (more or less) what it means: the source of the ComboBox is ActivePallets, and when choosing an MPNr (in the ComboBox), a corresponding CUnit will be found, and its PO and Description are shown in the TextBox.

The problem that I'm having, is that a Pallet is not uniquely defined by its MPNr, but by the combination of MPNr and PO.

My initial question was closed as a duplicate, because I did not understand that SelectedItem was not the key property (it's the combination of SelectedValue and SelectedValuePath, but now I'm still the issue that both MPNr and PO are to be used as input parameters, and I don't know how to do that.

For your information, in both Pallet and CUnit classes, the MPNr and PO are defined in a very simple way:

public int MPNr { get; set; }
public int PO   { get; set; }

In top of that, both Pallet and CUnit classes don't even have an explicit constructor.

How do I need to modify the definition of Pallet and/or CUnit, in order to be able to use both MPNr and PO as an input parameter for identification?

I already had a look at this similar question, but I don't understand how to apply in my situation.

1

There are 1 best solutions below

0
Dominique On BEST ANSWER

I've created a workaround, creating a new property, consisting of the concatenation of both properties:

public string MPNrPO { ...}
...
MPNrPO = $"{MPNr}|{PO}";

This is not a real solution: instead of understanding how to use two parameters as an input parameter, I combined both into one single parameter and used that one, but it gets the job done.

One very important, but very ugly remark on the workaround:

public string MPNrPO {
  get
  {
    return _mPNrPO;
  }
  set
  {
    if (value != null)
    _mPNrPO = value;
    else
    { } // Sometimes MPNrPO gets set to null 
        // (the callstack is meaningless, 
        //  root cause is lack of MVVM knowledge)
  }
}