c# - 绑定(bind)到依赖属性,而依赖属性又绑定(bind)到另一个绑定(bind)源

标签 c# .net wpf data-binding command

这很难解释,但我会尽力而为。 我想要一个具有 3 个按钮的可重用控件,一个用于创建实体,另一个用于编辑,另一个用于删除,这是相关部分的缩写 XAML。

--

<!-- ActionButtons.xaml -->

<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
        <Button Name="btnNew" Content="New" Command="{Binding Path=NewCommand}" />
        <Button Name="btnEdit" Content="Edit" Command="{Binding Path=EditCommand, Mode=OneWay}" />
        <Button Name="btnDelete" Content="Delete" Command="{Binding Path=DeleteCommand, Mode=OneWay}" />
    </StackPanel>

--

然后,在后面的代码中我有 dpprops 声明:

// ActionButtons.xaml.cs

public uscActionButtons()
{
            InitializeComponent();
            this.DataContext = this;
        }

        public ICommand NewCommand
        {
            get { return (ICommand)GetValue(NewCommandProperty); }
            set { SetValue(NewCommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for NewCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NewCommandProperty =
            DependencyProperty.Register("NewCommand", typeof(ICommand), typeof(uscActionButtons), new UIPropertyMetadata(null, new PropertyChangedCallback(OnCommandChanged)));

我想将 NewCommand 属性绑定(bind)到它在另一个控件中的特定实现。预期用途示例:

<!-- SomeControl.xaml -->
<common:uscActionButtons Grid.Row="0" HorizontalAlignment="Left" 
                                 NewCommand="{Binding NewItemCommand}"
                                 />

// SomeControlViewModel.cs
// Note: SomeControlViewModel IS in the DataContext of SomeControl.
public ICommand NewItemCommand
        {
            get
            {
                if (mNewItemCommand == null)
                {
                    mNewItemCommand = new RelayCommand(x => this.CreateItem());
                }

                return mNewItemGroupCommand;
            }
        }

问题是可重用控件 (ActionButtons) 没有看到 NewItemCommand。 如果我使用一个简单的按钮,它就可以正常显示。问题似乎出在这种“链式”绑定(bind)上。 但我知道这是可能的,WPF 按钮有一个 Command 依赖属性,您可以将命令绑定(bind)到该属性,因此创建我自己的公开 ICommand 依赖属性的可重用控件一定不会那么难。

有什么想法吗?

谢谢


编辑:这是解决方案,我所要做的就是将 RelativeSource 与 FindAncestor 一起使用。

<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
        <Button Name="btnNew" Content="New" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:uscActionButtons}, Path=NewCommand, Mode=OneWay}" />
        <Button Name="btnEdit" Content="Edit" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:uscActionButtons}, Path=EditCommand, Mode=OneWay}" />
        <Button Name="btnDelete" Content="Delete" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:uscActionButtons}, Path=DeleteCommand, Mode=OneWay}" />
    </StackPanel>

最佳答案

您看到的问题是您正在更改 ActionButtons 控件的 DataContext。当您在构造函数中设置其 DataContext 时,您在其上指定的所有 Bindings(甚至来自实例化它的外部 XAML)都将指向新的 DataContext。因此,当您在 SomeControl 中应用 Binding 时,该 Binding 试图绑定(bind)到 DataContext,而这恰好是 ActionButtons 实例。

我认为 Control 不应该设置自己的 DataContext,因为它会导致您所看到的错误。如果您想使用 UserControl(我可能会自己使用 Control,这样我就可以执行 TemplateBindings),那么您可以使用RelativeSource 绑定(bind)(正如您在对 Snowbear 的评论中提到的),Bindings 应该可以工作。

关于c# - 绑定(bind)到依赖属性,而依赖属性又绑定(bind)到另一个绑定(bind)源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4684597/

相关文章:

c# - 修改列单元格中的文本并使其成为超链接,listView WPF C#

c# - WPF 错误还是我要疯了?

c# - 文本框验证奇怪的行为

c# - 非数据类是模型的一部分?

.net - 如何使用 log4net 登录 RichTextBox?

.net - SignalR 集线器可扩展性问题

c# - 离开 StreamReader 而不关闭 Stream

c# - COM dll 的有效 .cs 文件

.net - 文件系统观察者

c# - XML 序列化 IOException 未处理