我是Wpf世界的新手,所以我创建了几个视图,所有这些视图都至少有一个ComboBox,因为我正在使用MvvM模式,我自己重新输入同一行的同一行代码填充组合并获取SelectedItem(创建属性,填充和其他的私有)。
是否有某种框架可以改善这一部分?或黑客/技巧???因为我看到太多重复的代码...也许我做错了什么,看看:
XAML:
<ComboBox name= "cbDepartments" DisplayMemberPath="DepartmentName"
SelectedValuePath ="PrimaryKey"
ItemsSource="{Binding Path=Departments}"
SelectedItem="{Binding Path=DefaultBranch,Mode=TwoWay}"
>
视图模型:
private Department defaultBranch;
public Department DefaultBranch
{
get
{
return this.defaultBranch;
}
set
{
if (this.defaultBranch != value)
{
this.defaultBranch = value;
this.OnPropertyChanged("DefaultBranch");
this.saveChangeCommand.RaiseCanExecuteChanged();
this.UserMessage = string.Empty;
}
}
}
private ObservableCollection<Department> departments;
public ObservableCollection<Department> Departments
{
get { return this.departments; }
set
{
if (this. departments!= value)
{
this. departments = value;
this.OnPropertyChanged("Departments");
}
}
}
答案 0 :(得分:1)
你拥有的大多数东西看起来很标准。你可以减少一些事情:
public ObservableCollection&lt; Department&gt;各部门{得到;私人集; }
答案 1 :(得分:0)
至于使用部门对组合框进行自定义控制 - 这在WPF中非常简单:
<ComboBox DisplayMemberPath="DepartmentName" x:Class="...DepartmentComboBox"
SelectedValuePath ="PrimaryKey"
ItemsSource="{Binding Path=Departments}"
SelectedItem="{Binding Path=DefaultBranch,Mode=TwoWay}"/>
代码隐藏:
public partial class DepartmentComboBox
{
public DepartmentComboBox()
{
InitializeComponent();
}
}