c# - WPF ComboBox 自定义绑定(bind)

标签 c# wpf binding combobox

我有一个自定义类,它有两个值,分别是描述和附加到 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"
                      />

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

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

关于c# - WPF ComboBox 自定义绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32282259/

相关文章:

c# - 使用一个可观察量来同步另一个可观察量

c# - 将 HttpResponseMessage.Content 数据 (ReadAsStringAsync) 限制为特定的最大大小

c# - 使用 FromAsyncPattern 进行单元测试

c# - 编辑 WriteableBitmap 的原始像素数据?

wpf - PathGeneratedInternally 标志在 WPF 绑定(bind)中的作用是什么?

c# - UITextField TextColor 属性绑定(bind)问题(MVVMCross)

c# - 如何集成 Expression<Func<>> 来清理我的 Linq-to-Entity 查询?

c# - 已处理的 RoutedEvent 继续冒泡树

wpf - 在样式中绑定(bind)命令

c# - ruby 中的 hmac-sha1 不同于 C# HMACSHA1