绑定到DependencyProperty仅适用于“MyValue”而不适用于“{Binding PropertyHoldingMyValue}”

时间:2012-09-07 13:45:19

标签: c# silverlight data-binding dependency-properties

我正在创建一个自定义的“PageHeaderControl”UserControl,其标题属性为:

 public partial class PageHeaderControl: UserControl
 {
    public static readonly DependencyProperty HeaderProperty =
        DependencyProperty.Register("Header",
                        typeof(string), typeof(PageHeaderControl),
                        new PropertyMetadata(""));

    public string Header
    {
        get { return GetValue(HeaderProperty) as string; }
        set { SetValue(HeaderProperty, value); }
    }

 }

在该控件的XAML中,我有:

<sdk:Label Content="{Binding Header,Mode=TwoWay}" />

现在出现问题:当我创建控件时,绑定它只能用于执行此操作:

<my:PageHeaderControl Header="This is my page header" />

这样做不起作用,其中PageHeader是我的ViewModel中保存标题值的属性:

<my:PageHeaderControl Header="{Binding PageHeader,Mode=TwoWay}" />

我想也许我的房产搞砸了,但这也有效:

<TextBlock Text="{Binding PageHeader,Mode=TwoWay}" />

关于问题可能是什么的任何想法!

非常感谢!!!

修改

在我的ViewModel中,PageHeader是这样的:

private string _pageHeader = "This is my page header";
public string PageHeader
{
    get
    {
        return _pageHeader;
    }
    set
    {
        _pageHeader = value;
        RaisePropertyChanged("PageHeader");
    }
}

编辑2:

当我在我的PageHeader属性的“get”中放置一个断点时,除非我添加了TextBlock,否则它不会被全部命中...

2 个答案:

答案 0 :(得分:2)

如果我理解正确,你试图将控件的XAML标记中元素的属性绑定到控件本身的属性。

如果是这种情况,请查看以下内容是否有帮助。

PageHeaderControl.xaml:

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

<Grid x:Name="LayoutRoot" Background="White">
    <TextBlock Text="{Binding Header, ElementName=MyControl}"></TextBlock>
</Grid>

PageHeaderControl.xaml.cs:

public partial class PageHeaderControl : UserControl
{
    public static readonly DependencyProperty HeaderProperty =
            DependencyProperty.Register("Header", typeof(string), typeof(PageHeaderControl), new PropertyMetadata(""));

    public string Header
    {
        get
        {
            return GetValue(HeaderProperty) as string;
        }
        set
        {
            SetValue(HeaderProperty, value);
        }
    }

    public PageHeaderControl()
    {
        InitializeComponent();
    }
}

ViewModel.cs:

public class ViewModel : INotifyPropertyChanged
{
    private string _pageHeader = "This is my page header";

    public string PageHeader
    {
        get
        {
            return _pageHeader;
        }
        set
        {
            _pageHeader = value;
            PropertyChanged(this, new PropertyChangedEventArgs("PageHeader"));
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
}

MainPage.xaml中:

<Grid x:Name="LayoutRoot" Background="White">
    <my:PageHeaderControl Header="{Binding PageHeader, Mode=TwoWay}"></my:PageHeaderControl>
</Grid>

MainPage.xaml.cs中:

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }
}

答案 1 :(得分:0)

我有点困惑,我认为你错过了Binding内联表达式的语法。

在“{Binding”之后来到你的财产的路径。 “PageHeader”是您的财产的路径吗?!

我认为你的意思是:

<my:PageHeader Header="{Binding PageHeader, Mode=TwoWay}" />

<TextBlock Text="{Binding PageHeader, Mode=TwoWay}" />

问题是Binding表达式仅在使用SetValue方法设置属性值时才有效,并通知父DependencyObject特定属性已更改! 您应该使用DependencyProperty对其进行TwoWay绑定,或者在类中实现System.ComponentModel.INotifyPropertyChange接口,并通过在接口中调用PropertyChanged事件手动通知Binding对象。

PageHeader属性的定义应如下所示:

public static readonly DependencyProperty PageHeaderProperty = DependencyProperty.Register("PageHeader", typeof(string), typeof(YOUROWNER), new PropertyMetadata(""));

public string PageHeader
{
    get { return GetValue(PageHeaderProperty) as string; }
    set { SetValue(PageHeaderProperty, value); }
}

干杯