4

我在自定义依赖属性绑定方面遇到了一些问题。我们有: 具有一个依赖属性并绑定到自身的自定义用户控件:

<UserControl x:Class="WpfApplication1.SomeUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
<Grid>
    <Label>
        <Label.Template>
            <ControlTemplate>
                <Label Content="{Binding MyTest}"/>
            </ControlTemplate>
        </Label.Template>
    </Label>
</Grid>

...和控制代码:

public partial class SomeUserControl : UserControl
{
    public SomeUserControl()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty MyTestProperty = DependencyProperty.Register("MyTest", typeof(int), typeof(SomeUserControl));

    public int MyTest
    {
        get { return (int)GetValue(MyTestProperty); }
        set { SetValue(MyTestProperty, value); }
    }
}

我试图使用这个控件绑定到简单模型类的一些简单属性:

<UserControl x:Class="WpfApplication1.AnotherUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:wpfApplication1="clr-namespace:WpfApplication1"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <wpfApplication1:SomeUserControl MyTest="{Binding Path=Model.MyNum}" Grid.Column="0"/>
    <Label Content="{Binding Path=Model.MyNum}" Grid.Column="1"/>
</Grid>

...带代码

public partial class AnotherUserControl : UserControl
{
    public MyModel Model { get; set; }
    public AnotherUserControl()
    {
        Model = new MyModel();
        Model.MyNum = 1231;
        InitializeComponent();
    }
}

...和型号:

public class MyModel:INotifyPropertyChanged
{
    private int _myNum;

    public int MyNum
    {
        get { return _myNum; }
        set { _myNum = value; OnPropertyChanged("MyNum");}
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

但是绑定不起作用。堆栈在编译中没有错误。现在,绑定使用 statndart wpf 标签控件(具有相同的模型),并且不使用我的自定义控件和自定义属性。请帮助我了解此问题的原因并解决它;谢谢)

4

2 回答 2

6

您应该在SomeUserControl.

<UserControl x:Class="WpfApplication1.SomeUserControl"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300" 
     x:Name="uc">

<Grid>
    <Label>
        <Label.Template>
            <ControlTemplate>
                <Label Content="{Binding MyTest, ElementName=uc}"/>
            </ControlTemplate>
        </Label.Template>
    </Label>
</Grid>

这里有更多信息为什么你不应该将用户控件的数据上下文设置为 self/this。 WPF 用户控件中的数据绑定

于 2013-10-09T06:00:40.087 回答
2

您需要按如下方式更新您的绑定,即您必须使用RelativeSource 绑定到祖先用户控件属性,因为您正在显式设置子用户控件的DataContext,默认绑定将在子用户控件的DataContext 中搜索路径。

        <wpfApplication1:SomeUserControl MyTest="{Binding Path=DataContext.Model.MyNum, RelativeSource={RelativeSource FindAncestor={x:Type UserControl}}}" Grid.Column="0"/>
于 2013-10-09T04:25:00.020 回答