ItemTemplate命令的WPF依赖属性

时间:2015-06-08 10:06:48

标签: wpf xaml dependency-properties

我有一个用户控件,为该控件定义ItemsControl和ItemTemplate,即

    <ItemsControl Name="ItemsControl">

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Name="SelectionButton" Content="MyButton"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>

    </ItemsControl>

在后面的代码中,我指定了一个依赖属性,它允许我绑定ItemsControl的ItemsSource属性,即

      public static readonly DependencyProperty ButtonSourceProperty = DependencyProperty.Register(
             "ButtonSource", typeof(IEnumerable), typeof(MyControl),
             new PropertyMetadata(null, new PropertyChangedCallback(OnButtonSourceChanged))); 

      public IEnumerable ButtonSource
      {
         get { return (IEnumerable)GetValue(ButtonSourceProperty); }
         set { SetValue(ButtonSourceProperty, value); }
      }

      private static void OnButtonSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
         var buttonSelectionControl = (ButtonSelectionControl)d;
         buttonSelectionControl.ItemsControl.ItemsSource = (IEnumerable)e.NewValue;
      }

      public static void SetButtonSource(DependencyObject obj, IEnumerable enumerable)
      {
         obj.SetValue(ButtonSourceProperty, enumerable);
      }

      public static IEnumerable GetButtonSource(DependencyObject obj)
      {
         return (IEnumerable)obj.GetValue(ButtonSourceProperty);
      }

这样在xaml中我可以设置MyControl的源代码如下

<local:MyControl ButtonSource={Binding MyCollection} \>

这有效,但是如何在MyControl中定义一个依赖属性,指定要在MyCollection中绑定的命令?目前,我在xaml中声明了以下命令绑定

Command="{Binding DataContext.MyCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                        CommandParameter="{Binding .}"

我如何以这样的方式对其进行抽象,即我可以将item命令设置为在xaml中绑定,例如:

<local:MyControl ButtonSource={Binding MyCollection}
                 ButtonCommand={Binding MyCommand} \>
指针赞赏。

2 个答案:

答案 0 :(得分:1)

确保您的UserControl具有ICommand的依赖属性,让我们说这被称为&#34; ButtonCommand&#34;。

您应该能够在模板内部为您的控件绑定:

<ItemsControl Name="ItemsControl">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Name="SelectionButton" Content="MyButton" 
                    Command="{Binding ButtonCommand, RelativeSource={RelativeSource AncestorType=wpfApplication1:UserControl1}}"
                    CommandParameter="{Binding}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

然后按下按钮应该在&#34; ButtonCommand&#34;中提升命令集。在用户控件中定义的依赖项属性。

您的ButtonCommand定义(在UserControl代码中)将如下所示:

public static readonly DependencyProperty ButtonCommandProperty = DependencyProperty.Register("ButtonCommand", typeof (ICommand), typeof (UserControl1), new PropertyMetadata(default(ICommand)));
public ICommand ButtonCommand { get { return (ICommand) GetValue(ButtonCommandProperty); } set { SetValue(ButtonCommandProperty, value); }}

创建一个实现ICommand的命令类是您可能知道的样板文件。将它放入按钮xaml:

CommandParameter="{Binding}"

..它允许您使用命令处理代码中列表中的项目:

public class TheCommand : ICommand
{
    public void Execute(object parameter)
    {
        var yourListItemObject = parameter as yourListItemObjectType;
    }

    // boilerplate stuff
    public bool CanExecute(object parameter) { return true; }
    public event EventHandler CanExecuteChanged;
}

答案 1 :(得分:0)

您可以在UserContol的.cs文件中定义2个依赖项属性ButtonCommmandButtonCommandParameter,并将它们绑定在UserControl的xaml中,如下所示:

<UserControl x:Class="..."
             x:Name="this">
    <Button Command="{Binding ButtonCommand, ElementName=this}" 
            CommandParameter="{Binding ButtonCommandPrameter, ElementName=this}"/>
</UserControl>