你有依赖属性也有绑定吗?

时间:2013-11-17 23:59:01

标签: wpf binding dependency-properties attached-properties

您是否可以拥有也具有绑定的依赖项属性?

例如:

<UniformGrid Rows="2">
    <Button>
        <l:AttachedProperties.Data>
            <l:Data ParamA="1"
                    ParamB="2" />
        </l:AttachedProperties.Data> >
    </Button>

    <Button>
        <l:AttachedProperties.Data>
            <l:Data ParamA="{Binding A}"
                    ParamB="{Binding B}" />
        </l:AttachedProperties.Data> >
    </Button>
</UniformGrid>

我尝试使用以下代码,但是在输出窗口中出现绑定错误,通知我:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=A; DataItem=null; target element is 'Data' (HashCode=26756241); target property is 'ParamA' (type 'Int32')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=B; DataItem=null; target element is 'Data' (HashCode=26756241); target property is 'ParamB' (type 'Int32')

所以我假设上下文没有被继承。可能吗?

public class Data : DependencyObject
{
    public int ParamA
    {
        get { return (int)GetValue(ParamAProperty); }
        set { SetValue(ParamAProperty, value); }
    }

    public static readonly DependencyProperty ParamAProperty =
        DependencyProperty.Register("ParamA", typeof(int), typeof(Data), new PropertyMetadata(-1));

    public int ParamB
    {
        get { return (int)GetValue(ParamBProperty); }
        set { SetValue(ParamBProperty, value); }
    }

    public static readonly DependencyProperty ParamBProperty =
        DependencyProperty.Register("ParamB", typeof(int), typeof(Data), new PropertyMetadata(-1));

}

public static class AttachedProperties
{
    public static readonly DependencyProperty DataProperty
        = DependencyProperty.RegisterAttached("Events", typeof(Data), typeof(AttachedProperties),  new PropertyMetadata(null));

    public static void SetData(UIElement element, Data value)
    {
        element.SetValue(DataProperty, value);
    }

    public static Data GetData(UIElement element)
    {
        return (Data)element.GetValue(DataProperty);
    }
}

public partial class Window1 : Window
{
    public int A { get { return 1; } }
    public int B { get { return 2; } }

    public Window1()
    {
        DataContext = this;
        InitializeComponent();
    }
}

    <Window x:Class="WpfApplication102.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:l="clr-namespace:WpfApplication102"
        Title="Window1" Height="300" Width="300">

        <UniformGrid Rows="4">
            <TextBlock Text="{Binding A}" />
            <TextBlock Text="{Binding B}" />
            <Button>
                <l:AttachedProperties.Data>
                    <l:Data ParamA="1"
                            ParamB="2" />
                </l:AttachedProperties.Data> >
            </Button>

            <Button>
                <l:AttachedProperties.Data>
                    <l:Data ParamA="{Binding A}"
                            ParamB="{Binding B}" />
                </l:AttachedProperties.Data> >
            </Button>
        </UniformGrid>

    </Window>

0 个答案:

没有答案
相关问题