要编辑绑定到自定义对象列表的WPF ComboBox项的属性,您可以按照以下步骤进行操作:
创建自定义对象类:
public class CustomObject
{
public string Name { get; set; }
public string Value { get; set; }
}
在XAML中创建ComboBox控件:
在ViewModel中创建CustomObjects属性和SelectedCustomObject属性:
public class ViewModel : INotifyPropertyChanged
{
public ObservableCollection CustomObjects { get; set; }
public CustomObject SelectedCustomObject { get; set; }
public ViewModel()
{
CustomObjects = new ObservableCollection
{
new CustomObject { Name = "Object 1", Value = "Value 1" },
new CustomObject { Name = "Object 2", Value = "Value 2" },
new CustomObject { Name = "Object 3", Value = "Value 3" }
};
}
// INotifyPropertyChanged implementation
}
在您的窗口或用户控件的代码中设置DataContext:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
现在,您可以在ComboBox中显示CustomObjects的名称,并且选择的项将绑定到SelectedCustomObject属性。您可以通过在ViewModel中处理SelectedCustomObject的更改来对其进行编辑或处理。
上一篇:边际百分比值是如何计算的?
下一篇:编辑保存在文件中的字典值