【问题标题】:c# wpf Combox value in editable field must be different that displaymemberpathc# wpf 可编辑字段中的组合框值必须不同于 displaymemberpath
【发布时间】:2015-10-12 16:39:19
【问题描述】:

我在 displaymemberpath 中有一个带有连接字符串的组合框(下面代码隐藏中的“DescriptionComplete”),这就是我在可编辑字段中得到的,但这只是我想要进入可编辑字段的 IdEchantillon... 请问我该怎么做?

XAML:

<ComboBox x:Name="cbEchantillon" SelectedValue="{Binding CurrentEchantillon.IdEchantillon}" ItemsSource="{Binding OcEchantillon}" DisplayMemberPath="DescriptionComplete" SelectedValuePath="IdEchantillon" SelectedItem="{Binding CurrentEchantillon}" Text="{Binding IdEchantillon}" IsEditable="True" Width="355" FontSize="14" FontFamily="Courier New" SelectionChanged="cbEchantillon_SelectionChanged"></ComboBox>

后面的代码:

public class Echantillon : ViewModelBase
    {
        private string _IdEchantillon;
        private string _Description;
        private DateTime _dateechan;
        private string _descriptionComplete;
}
    public string IdEchantillon
    {
        get { return _IdEchantillon; }
        set { _IdEchantillon = value; RaisePropertyChanged("IdEchantillon"); }
    }


    public string Description
    {
        get { return _Description; }
        set { _Description = value; RaisePropertyChanged("Description"); }
    }

    public DateTime Dateechan
    {
        get { return _dateechan; }
        set { _dateechan = value; RaisePropertyChanged("Dateechan"); }
    }

    public string DescriptionComplete
    {
        get { return string.Format("{0} {1} {2}", IdEchantillon.PadRight(20), Dateechan.ToShortDateString(), Description); }
        set { _descriptionComplete = value; }
    }

}

【问题讨论】:

  • 不清楚你在问什么。试着一步一步解释,如果你英语不是很好,就一步一步来。不要试图缩短你的话,很难理解你在这里真正想要什么。

标签: c# wpf xaml combobox selectedvalue


【解决方案1】:

首先,请清理您的 ComboBox 属性绑定。您不应该为SelectedValue 创建绑定,因为您已经拥有SelectedItem 属性的绑定。 SelectedValue 是通过从SelectedItem 中提取SelectedValuePath 的值来确定的。

在绑定到 ViewModel 的情况下,您不应同时绑定 Text 属性。像您一样使用ItemsSource,并将DisplayMemberPath 属性设置为您希望显示为绑定集合中每个项目的文本表示形式。

【讨论】:

    猜你喜欢
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 2021-02-04
    • 2018-01-10
    相关资源
    最近更新 更多