我有一个自定义类,其中两个值是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字段。
请帮忙!
答案 0 :(得分: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"/>