wpf - XAML:如何将 DependencyProperty 绑定(bind)到不同对象上同名的 C# 属性?

标签 wpf xaml dependency-properties

使用:

  • Visual Studio 社区版 2015
  • .Net 4.0

我已经实现了this answer ,生成我自己的 CheckBox 类,并带有 IsChecked DependencyProperty。该属性由 WPF CheckBox 上的 IsChecked 属性支持,或者如果它可以工作的话。工作意味着当复选框被切换时我的 getter 和 setter 被调用。

如果我将属性重命名为 IsChecked_temp 并修改 XAML 以匹配,则它可以正常工作。我认为这是命名冲突,但为什么 ElementName 没有解决它?我的最小测试用例如下。

编辑0:我忘了提及,我没有收到任何错误或警告。

编辑1:This answer最初被接受是因为它适用于测试用例,但显然不是完整的答案。将其应用到我的项目(并将 CheckBox 类重命名为 ToggleBox)会在每次使用该属性时产生 XamlParseException:

A 'Binding' cannot be set on the 'IsChecked' property of type 'ToggleBox'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

我将尝试获得一个最小的测试用例来展示这一点。

CheckBox.xaml

<UserControl x:Class="CheckBox_test.CheckBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Name="Self">
    <StackPanel>
        <CheckBox IsChecked="{Binding IsChecked, ElementName=Self}" />
    </StackPanel>
</UserControl>

CheckBox.xaml.cs

using System.Windows;
using System.Windows.Controls;

namespace CheckBox_test
{
    public partial class CheckBox : UserControl
    {
        public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register(
             "IsChecked",
             typeof(bool),
             typeof(CheckBox),
             new FrameworkPropertyMetadata(false,
                     FrameworkPropertyMetadataOptions.AffectsRender));

        public bool IsChecked
        {
            get { return (bool)GetValue(IsCheckedProperty); }
            set { SetValue(IsCheckedProperty, value); }
        }

        public CheckBox()
        {
            InitializeComponent();
        }
    }
}

MainWindow.xaml

<Window x:Class="CheckBox_test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:CheckBox_test">
    <Grid>
        <local:CheckBox />
    </Grid>
</Window>

MainWindow.xaml.cs(为了完整性)

using System.Windows;

namespace CheckBox_test
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

最佳答案

非常有趣的问题(至少对我来说),所以事实证明,当您注册依赖项属性时,确实存在名称冲突。

我不太确定这是否是一个答案,但我想如果您之前不知道或考虑过它,您会发现这很有趣。

我使用了“CheckBox.IsChecked”,但任何唯一的名称可能就足够了。

public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register(
         "CheckBox.IsChecked",
         typeof(bool),
         typeof(CheckBox),
         new FrameworkPropertyMetadata(false,
                 FrameworkPropertyMetadataOptions.AffectsRender));

无需更改您的属性(property)名称即可使用

public bool IsChecked
{
    get
    {
        return (bool)GetValue(IsCheckedProperty);
    }
    set
    {
        SetValue(IsCheckedProperty, value);
    }
}

When you create names for your dependency properties, you must choose unique names that are not being used for dependency properties or events in any base classes that you inherit from; otherwise, an ArgumentException is thrown during runtime. For more information about dependency properties and activity binding, see Custom Activity Binding Sample and Simple Activity Sample.

https://msdn.microsoft.com/en-us/library/vstudio/ms734499(v=vs.90).aspx

再次提醒我我是一个多么大的菜鸟:)

关于wpf - XAML:如何将 DependencyProperty 绑定(bind)到不同对象上同名的 C# 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33424109/

相关文章:

c# - 在带有 Entity Framework 的 ViewModel 上使用 DependencyProperty

c# - WP7 : Binding to attached properties

wpf - 如何 'get at' WPF 组合框 PART_EditableTextbox 因为组合框没有突出显示?

xaml - 如何在 StandardStyles.xaml 中定义基元

c# - MVVM : Binding Command with Observable collection to Listbox and taking values from textbox

c# - 如何在 WinRT 中的按钮中将文本放置在图像上

c# - 如何在 WinRT XAML C# 中克隆 UIElement?

wpf - 依赖属性 - 如何添加所有者以使其充当附加属性?

c# - 设置数据网格单元格背景颜色wpf

wpf - 如何通过 MVVM 为 DataGrid ItemSsource 设置过滤器