从XAML绑定到自定义依赖属性

3
我已经实现了自定义DependencyProperty,并希望从XAML绑定它。但不知何故,当绑定源(MainWindow.Test)更新时,它并未更新。绑定源不是DP,但触发了PropertyChanged事件。但使用非自定义依赖属性时更新是有效的。
<TextBlock Text="{Binding Test}" />

无法工作:

<local:DpTest Text="{Binding Test}"/>

有什么想法吗?


这里是DP实现::

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

namespace WpfApplication3
{
public partial class DpTest : UserControl
{
    public DpTest()
    {
        DataContext = this;
        InitializeComponent();
    }

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(DpTest), new PropertyMetadata(string.Empty, textChangedCallBack));

    static void textChangedCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args)
    {
        int x = 5;
    }
}
}

这是它的使用方法:

<Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:WpfApplication3" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <TextBlock Text="{Binding Test}"></TextBlock>
    <local:DpTest Text="{Binding Test}"></local:DpTest>
    <Button Click="Button_Click">Update</Button>
</StackPanel></Window>

使用数据绑定源的代码后置:

using System;
using System.Collections.Generic;
using System.Windows;
using System.ComponentModel;

namespace WpfApplication3
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        DataContext = this;
        InitializeComponent();
    }
    string _test;
    public string Test
    {
        get { return _test; }
        set
        {
            _test = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Test"));
            }
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Test = "Updatet Text";
    }
    public event PropertyChangedEventHandler PropertyChanged;
}
}

你有检查输出窗口中的绑定错误吗? - Thomas Levesque
2个回答

3
不要在UserControls上设置DataContext=this;,这样如果您假定DataContext被继承,实例上的所有绑定都将失败,因为它会阻止继承并且非常难以察觉。在UserControl的绑定中,你应该使用 ElementName 进行控件命名,并进行绑定或使用 RelativeSource。例如:
<UserControl Name="control" ...>
     <TextBlock Text="{Binding Text, ElementName=control}"/>

<UserControl ...>
     <TextBlock Text="{Binding Text, RelativeSource={RelativeSource AncestorType=UserControl}}"/>

0
给你的窗口命名,并将绑定更改为以下内容: (否则,UserControl 的数据上下文将被使用,这不是你在这种情况下想要的)
...
x:Name="mainWindow">
        <Grid>
        <StackPanel>
          <TextBlock Height="20" Background="Yellow" Text="{Binding Test}"></TextBlock>
          <local:DpTest Text="{Binding Path=Test, ElementName=mainWindow, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></local:DpTest>
          <Button Click="Button_Click">Update</Button>

如果您的UserControl中还没有绑定,那么请按照以下方式进行绑定:
<TextBlock Text="{Binding ElementName=dpTest, Path=Text,
Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBlock>

这对我有用...

希望有所帮助


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接