【问题标题】:Binding to DependencyProperty works only with "MyValue" and not "{Binding PropertyHoldingMyValue}"绑定到 DependencyProperty 仅适用于“MyValue”,不适用于“{Binding PropertyHoldingMyValue}”
【发布时间】:2012-09-01 09:53:35
【问题描述】:

我正在创建一个带有标题属性的自定义“PageHeaderControl”用户控件:

 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 中添加...

【问题讨论】:

  • 您是否尝试过使用toolkit 中的HeaderedContentControl?这就是它的目的

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


【解决方案1】:

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

在“{Binding”之后是指向您的财产的路径。 “PageHeader”是您属性的路径吗?!

我认为你的意思是:

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

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

问题在于,Binding 表达式仅在您使用 SetValue 方法设置属性值并通知父 DependencyObject 特定属性已更改时才有效! 您应该使用 DependencyProperty 对其进行双向绑定,或者在您的类中实现 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); }
}

干杯

【讨论】:

  • 根据问题,在TextBlock 中绑定到PageHeader 确实有效。我假设 OP 的视图模型中有一个名为 PageHeader 的属性,其中包含要在 my:PageHeader 控件中显示的文本 (?)
  • PageHeader 是我的 ViewModel 中属性的路径,抱歉我没说清楚。
  • 在 ViewModel 中包含 PageHeader 属性的定义会更好。
  • 但是 DependencyProperty 是在我的控制之下,而不是我的视图模型...有没有办法让 DP 远离视图模型?
  • 如果 TwoWay 绑定的目标对象是您的模型,那么它应该实现上述接口或者是一个 DependencyObject。但如果 TwoWay 绑定的目标对象是控件,则应更改绑定表达式并将它们绑定到控件的属性。我认为您的问题是模型问题,而不是技术问题。干杯
【解决方案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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 2017-06-19
    相关资源
    最近更新 更多