繁体   English   中英

WPF ComboBox 自定义绑定

[英]WPF ComboBox Custom Binding

我有一个自定义类,其中有两个值是 description 和 url,它附加到 ComboBox。 ComboBox 附加到类并按预期填充,但我无法使用 url 值来填充 ComboBox 的 text 属性。

            <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 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"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM