gpt4 book ai didi

ItemTemplate 命令的 WPF 依赖属性

转载 作者:行者123 更新时间:2023-12-04 22:20:22 28 4
gpt4 key购买 nike

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

    <ItemsControl Name="ItemsControl">

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

</ItemsControl>

在后面的代码中,我指定了一个依赖属性,使我能够绑定(bind) 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 中绑定(bind)的命令?目前我在 xaml 中为命令绑定(bind)声明了以下内容

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

我怎样才能以这样一种方式抽象它,我可以在 xaml 中设置要绑定(bind)的项目命令,比如:

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

感谢指点。

最佳答案

确保您的 UserControl 具有对 ICommand 的依赖属性,假设这称为“ButtonCommand”。

你应该能够在你的控件的模板中绑定(bind)到这个:

<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>

然后单击按钮应该引发在用户控件中定义的“ButtonCommand”依赖属性中的命令集。

您的 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;
}

关于ItemTemplate 命令的 WPF 依赖属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30706328/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com