【问题标题】:WPF ComboBox Custom BindingWPF ComboBox 自定义绑定
【发布时间】:2015-08-29 02:55:53
【问题描述】:

我有一个自定义类,其中包含两个值,即附加到 ComboBox 的描述和 url。 ComboBox 附加到类并按预期填充,但我不能使用 url 值来填充 ComboBox 的文本属性。

            <ComboBox x:Name="platform_url" Grid.Column="1"  HorizontalAlignment="Left" Height="Auto" Margin="3" Grid.Row="0" VerticalAlignment="Center" Width="120"
                    Text="{Binding url, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"
                    ItemsSource="{Binding LocationList}"
                    SelectedItem="{Binding SelectedLocation}"
                      DisplayMemberPath="Description"
                    SelectedValuePath="Url"
                      />

使用以下类进行数据填充和控制:

class LocationViewModel : INotifyPropertyChanged
{
    public ObservableCollection<Location> LocationList { get; set; }
    public LocationViewModel()
    {
        LocationList = new ObservableCollection<Location>();
        LocationList.Add(new Location() { Description = "North America", Url = "abc.com" });
    }

    private Location selectedLocation;
    public Location SelectedLocation
    {
        get { return selectedLocation; }
        set
        {
            selectedLocation = value;
            OnPropertyChanged("SelectedLocation");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}
class Location
{
    public string Url { get; set; }
    public string Description { get; set; }
}

我能得到的最好的是正在使用的描述字段,而不是 url 字段。

请帮忙!

【问题讨论】:

  • 您希望如何在您的ComboBox 中同时显示DescriptionUrl 属性?

标签: c# wpf binding combobox


【解决方案1】:

搞定了

                    <ComboBox x:Name="platform_datacenter" Grid.Column="1"  HorizontalAlignment="Left" Height="Auto" Margin="3" Grid.Row="0" VerticalAlignment="Center" Width="100"
                        SelectedValuePath="moid"
                        SelectedValue="{Binding Path=datacenter, Mode=TwoWay}" 
                        DisplayMemberPath="datacenter"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    相关资源
    最近更新 更多