如何 select ComboBox 中由对象填充的对象实例
How to select an instance of object from ComboBox that filled by objects
这个问题可能很愚蠢,但我是 WPF 的新手,所以很抱歉。
我在这里发现了很多类似的问题,但其中 none 有帮助。
我有一个表单可以获取一些带有 Location
属性 的对象作为输入。
我希望在显示表单时在 ComboBox
中选择此 Location
属性。
我还想让这个 属性 在表单的生命周期内可以更改。
我是这样绑定的:
<ComboBox Name="CB_Location" ItemsSource="{Binding Locations}" SelectedItem="{Binding SelectedLocation}" DisplayMemberPath="LocationName" SelectedValuePath="LocationID" SelectionChanged="CB_Location_SelectionChanged"/>
public class Location
{
public int LocationID { get; set; }
public string LocationName { get; set; }
}
public Form(object _obj)
{
InitializeComponent();
lctrl = new LocationController();
Locations = lctrl.GetAllLocations();
SelectedLocation = lctrl.GetLocationById(_obj.LocationID);
DataContext = this;
}
public List<Location> Locations { get; set; }
public Location SelectedLocation; { get; set; }
ComboBox
正确填充了对象,但我无法正确设置 SelectedItem
属性。
没有设置选中项的问题是因为SelectedLocation
是一个字段,不能绑定。关于绑定源的更多信息,可以参考documentation.
You can bind to public properties, sub-properties, as well as indexers, of any common language runtime (CLR) object. The binding engine uses CLR reflection to get the values of the properties. Alternatively, objects that implement ICustomTypeDescriptor or have a registered TypeDescriptionProvider also work with the binding engine.
为了使您当前的代码正常工作,只需将 SelectedLocation
设为 public 属性.
public Location SelectedLocation { get; set; }
除此之外,如果你只想绑定选中的item,设置一个SelectedValuePath
是没用的
<ComboBox Name="CB_Location"
ItemsSource="{Binding Locations}"
SelectedItem="{Binding SelectedLocation}"
DisplayMemberPath="LocationName"
SelectionChanged="CB_Location_SelectionChanged"/>
如果您想将 SelectedValue
绑定到 SelectedValuePath
适用的位置,则必须公开与所选值路径类型匹配的 属性,此处 int
.
public int SelectedLocationID { get; set; }
然后您可以将 SelectedValue
与值路径 LocationID
绑定(没有 SelectedItem
)。
<ComboBox Name="CB_Location"
ItemsSource="{Binding Locations}"
DisplayMemberPath="LocationName"
SelectedValuePath="LocationID"
SelectedValue="{Binding SelectedLocationID}"
SelectionChanged="CB_Location_SelectionChanged"/>
关于更新属性的另一说明。看来你没有实现 INotifyPropertyChanged
接口。例如,如果您设置 Location
,用户界面(此处为 ComboBox
)将不会反映更改,因为它不会收到通知。因此,如果您打算更改表单中绑定的 Location
或其他属性,则必须 implement INotifyPropertyChanged
,例如:
public class YourViewModel : INotifyPropertyChanged
{
private Location _selectedLocation;
public Location SelectedLocation
{
get => _selectedLocation;
set
{
if (_selectedLocation == value)
return;
_selectedLocation = value;
OnPropertyChanged();
}
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
// ...other code.
}
这个问题可能很愚蠢,但我是 WPF 的新手,所以很抱歉。 我在这里发现了很多类似的问题,但其中 none 有帮助。
我有一个表单可以获取一些带有 Location
属性 的对象作为输入。
我希望在显示表单时在 ComboBox
中选择此 Location
属性。
我还想让这个 属性 在表单的生命周期内可以更改。
我是这样绑定的:
<ComboBox Name="CB_Location" ItemsSource="{Binding Locations}" SelectedItem="{Binding SelectedLocation}" DisplayMemberPath="LocationName" SelectedValuePath="LocationID" SelectionChanged="CB_Location_SelectionChanged"/>
public class Location
{
public int LocationID { get; set; }
public string LocationName { get; set; }
}
public Form(object _obj)
{
InitializeComponent();
lctrl = new LocationController();
Locations = lctrl.GetAllLocations();
SelectedLocation = lctrl.GetLocationById(_obj.LocationID);
DataContext = this;
}
public List<Location> Locations { get; set; }
public Location SelectedLocation; { get; set; }
ComboBox
正确填充了对象,但我无法正确设置 SelectedItem
属性。
没有设置选中项的问题是因为SelectedLocation
是一个字段,不能绑定。关于绑定源的更多信息,可以参考documentation.
You can bind to public properties, sub-properties, as well as indexers, of any common language runtime (CLR) object. The binding engine uses CLR reflection to get the values of the properties. Alternatively, objects that implement ICustomTypeDescriptor or have a registered TypeDescriptionProvider also work with the binding engine.
为了使您当前的代码正常工作,只需将 SelectedLocation
设为 public 属性.
public Location SelectedLocation { get; set; }
除此之外,如果你只想绑定选中的item,设置一个SelectedValuePath
是没用的
<ComboBox Name="CB_Location"
ItemsSource="{Binding Locations}"
SelectedItem="{Binding SelectedLocation}"
DisplayMemberPath="LocationName"
SelectionChanged="CB_Location_SelectionChanged"/>
如果您想将 SelectedValue
绑定到 SelectedValuePath
适用的位置,则必须公开与所选值路径类型匹配的 属性,此处 int
.
public int SelectedLocationID { get; set; }
然后您可以将 SelectedValue
与值路径 LocationID
绑定(没有 SelectedItem
)。
<ComboBox Name="CB_Location"
ItemsSource="{Binding Locations}"
DisplayMemberPath="LocationName"
SelectedValuePath="LocationID"
SelectedValue="{Binding SelectedLocationID}"
SelectionChanged="CB_Location_SelectionChanged"/>
关于更新属性的另一说明。看来你没有实现 INotifyPropertyChanged
接口。例如,如果您设置 Location
,用户界面(此处为 ComboBox
)将不会反映更改,因为它不会收到通知。因此,如果您打算更改表单中绑定的 Location
或其他属性,则必须 implement INotifyPropertyChanged
,例如:
public class YourViewModel : INotifyPropertyChanged
{
private Location _selectedLocation;
public Location SelectedLocation
{
get => _selectedLocation;
set
{
if (_selectedLocation == value)
return;
_selectedLocation = value;
OnPropertyChanged();
}
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
// ...other code.
}