0

I'm trying to bind the ComboBox to show both the Key and the Value property of my Details list, on which this ComboBox.ItemSource should bind. example

List<obj> data = new List<obj> 
{
{Key = 1, Value = "Hight"},
{Key = 2, Value = "Normal"},
{Key = 3, Value = "Low"}
}

Combobox▼

「1 Hight」

「2 Normal」

「3 Low」

After selecting, only 「1」 or 「hight」 is displayed, depending on combobox type. My below approach works fine, but when the combobox count starts to increase, they no longer work as smoothly as I expected.

Sometimes it will show 「1 Hight」 instead of 1, sometimes select combobox A like combobox A and B both change and show 「1 Hight」 after selecting. What did I do wrong, please help me.

My approach.

ItemModel

public class Item : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private String _key { set; get; } = string.Empty;
    public String Key
    {
        get => _key;
        set
        {
            _key = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Key)));
        }
    }
    private String _value { set; get; } = string.Empty;
    public String Value
    {
        get => _value;
        set
        {
            _value = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value)));
        }
    }
    public Item() { }
    public Item(string key, string value)
    {
        Key = key;
        Value = value;
    }
    //1⇒set background red
    private bool _flag;
    public bool Flag
    {
        get => _flag;
        set
        {
            _flag = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Flag)));
        }
    }

    public string DisplayCustumer
    {
        private set { }
        get
        {
            return string.Format("{0} {1}", Key, Value);
        }
    }
}

ViewModel

public class ucMainteViewModel : BindableBase, IRegionMemberLifetime
{
    public ReactiveProperty<string> DisplayMemberDefaultKey { get; } = new ReactiveProperty<string>("Key");
    public ReactiveProperty<string> DisplayMemberDefaultValue { get; } = new ReactiveProperty<string>("Value");

    public ReactiveCommand ComboboxOpenedCommand { get; }
    public ReactiveCommand ComboboxClosedCommand { get; }

    public ucMainteViewModel()
    {
        ComboboxOpenedCommand = new ReactiveCommand();
        ComboboxOpenedCommand.Subscribe((param) => ComboboxOpened(param.ToString()));
        ComboboxClosedCommand = new ReactiveCommand();
        ComboboxClosedCommand.Subscribe(param => ComboboxClosed(param.ToString()));
    }
    private void ComboboxOpened(string property)
    {
        var obj = this.GetType().GetProperty(property).GetValue(this);
        if (obj == null) return;
        obj.GetType().GetProperty("Value").SetValue(obj, "DisplayCustumer");
    }
    private void ComboboxClosed(string property)
    {
        var obj = this.GetType().GetProperty(property).GetValue(this);
        if (obj == null) return;
        obj.GetType().GetProperty("Value").SetValue(obj, property.Contains("Key") ? "Key" : "Value");
        Thread.Sleep(TimeSpan.FromMilliseconds(200));
        obj.GetType().GetProperty("Value").SetValue(obj, property.Contains("Key") ? "Key" : "Value");
    }
}

View(xaml)

<ComboBox x:Name="cbo1_11"
          IsEditable="True"
          SelectedItem="{Binding 総合判定.Value}"
          ItemsSource="{Binding 判定Items}"
          DisplayMemberPath="{Binding DisplayMemberDefaultKey.Value, UpdateSourceTrigger=LostFocus, Mode=TwoWay}"
          Style="{Binding Path=総合判定.Value, Converter={StaticResource StyleConverter}}"
          ToolTip="{Binding Path=(Validation.Errors)/ErrorContent, RelativeSource={RelativeSource Self}}"
          Grid.Row="2"
          Grid.Column="1"
          HorizontalContentAlignment="Stretch"
          FontSize="13"
          VerticalContentAlignment="Center"
          Background="White">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="DropDownOpened"
                        SourceObject="{Binding ElementName=cbo1_11}">
            <i:InvokeCommandAction Command="{Binding ComboboxOpenedCommand}" CommandParameter="DisplayMemberDefaultKey"/>
        </i:EventTrigger>
        <i:EventTrigger EventName="DropDownClosed"
                        SourceObject="{Binding ElementName=cbo1_11}">
            <i:InvokeCommandAction Command="{Binding ComboboxClosedCommand}" CommandParameter="DisplayMemberDefaultKey"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>

<ComboBox x:Name="cbo2_11"
          IsEditable="True"
          SelectedItem="{Binding 聴力.Value, UpdateSourceTrigger=PropertyChanged}"
          ItemsSource="{Binding 聴力Items}"
          DisplayMemberPath="{Binding DisplayMemberDefaultValue.Value, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
          Style="{Binding Path=聴力.Value, Converter={StaticResource StyleConverter}}"
          ToolTip="{Binding Path=(Validation.Errors)/ErrorContent, RelativeSource={RelativeSource Self}}"
          Grid.Row="2"
          Grid.Column="1"
          HorizontalContentAlignment="Stretch"
          FontSize="13"
          VerticalContentAlignment="Center"
          Background="White">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="DropDownOpened"
                        SourceObject="{Binding ElementName=cbo2_11}">
            <i:InvokeCommandAction Command="{Binding ComboboxOpenedCommand}" CommandParameter="DisplayMemberDefaultValue" />
        </i:EventTrigger>
        <i:EventTrigger EventName="DropDownClosed"
                        SourceObject="{Binding ElementName=cbo2_11}">
            <i:InvokeCommandAction Command="{Binding ComboboxClosedCommand}" CommandParameter="DisplayMemberDefaultValue" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>
hungnt03
  • 61
  • 5

0 Answers0