C# 创建包含命令绑定的ListView用户控件
我的应用程序中的多个视图包含一个C# 创建包含命令绑定的ListView用户控件,c#,wpf,listview,mvvm,C#,Wpf,Listview,Mvvm,我的应用程序中的多个视图包含一个列表视图,它们之间的区别是项目资源,列表视图也绑定了一些命令,这些命令在视图模型中的实现是相同的。我想创建一个UserControl并在用户控件的视图模型中处理该命令,这可以删除多视图视图模型中的大量代码 我的UserControl如下所示: public partial class CustomListViewControl : UserControl { public static readonly DependencyProperty MyItems
列表视图
,它们之间的区别是项目资源
,列表视图
也绑定了一些命令,这些命令在视图模型中的实现是相同的。我想创建一个UserControl
并在用户控件的视图模型中处理该命令,这可以删除多视图视图模型中的大量代码
我的UserControl
如下所示:
public partial class CustomListViewControl : UserControl
{
public static readonly DependencyProperty MyItemsSourceProperty = DependencyProperty.Register("MyItemsSource", typeof(IEnumerable),
typeof(CustomListViewControl));
public CustomListViewControl()
{
InitializeComponent();
}
public IEnumerable MyItemsSource
{
get { return (IEnumerable)GetValue(CustomListViewControl.MyItemsSourceProperty); }
set { SetValue(CustomListViewControl.MyItemsSourceProperty, value); }
}
}
但是在命令中,我需要使用
ItemsSource
。如何将DependencyProperty
挂接到用户控件的视图模型中的可观察集合
?您需要公开iCommand参数以及iCommand本身的依赖属性
/// <summary>
/// BUTTON COMMAND: The iCommand of the button
/// </summary>
/// <remarks></remarks>
public ICommand ButtonCommand {
get { return GetValue(ButtonCommandProperty); }
set { SetValue(ButtonCommandProperty, value); }
}
public static readonly DependencyProperty ButtonCommandProperty = DependencyProperty.Register("ButtonCommand", typeof(ICommand), typeof(CustomListViewControl ), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
/// <summary>
/// BUTTON COMMAND: The iCommandParameter of the button
/// </summary>
/// <remarks></remarks>
public ObservableCollection<T> ButtonCommandParameter {
get { return GetValue(ButtonCommandParameterProperty); }
set { SetValue(ButtonCommandParameterProperty, value); }
}
public static readonly DependencyProperty ButtonCommandParameterProperty = DependencyProperty.Register("ButtonCommandParameter", typeof(ObservableCollection<T>), typeof(CustomListViewControl), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
//
///按钮命令:按钮的图标命令
///
///
公用ICommand按钮命令{
获取{返回GetValue(ButtonCommandProperty);}
set{SetValue(ButtonCommandProperty,value);}
}
公共静态只读DependencyProperty ButtonCommandProperty=DependencyProperty.Register(“ButtonCommand”、typeof(ICommand)、typeof(CustomListViewControl)、new FrameworkPropertyMetadata(null,FrameworkPropertyMetadata Options.AffectsRender));
///
///按钮命令:按钮的ICommand参数
///
///
公共ObservableCollection按钮命令参数{
获取{返回GetValue(ButtonCommandParameterProperty);}
set{SetValue(ButtonCommandParameterProperty,value);}
}
公共静态只读DependencyProperty ButtonCommandParameterProperty=DependencyProperty.Register(“ButtonCommandParameter”、typeof(ObservableCollection)、typeof(CustomListViewControl)、new FrameworkPropertyMetadata(null,FrameworkPropertyMetadata Options.AffectsRender));
现在您应该能够绑定到命令(本例中为ButtonCommand),然后将itemssource的listview可观察集合绑定到ButtonCommandParameter
我使用了这种精确的方法,它在WPF4.5中工作。我从VB转换,所以你可能需要清理一下
希望这有帮助。如果我理解你的问题,我不确定,但是你能不能做…MyItemSource={Binding YourObservableCollection}…
?