【发布时间】: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中同时显示Description和Url属性?