c# - 绑定(bind)到 DependencyProperty 仅适用于 "MyValue",不适用于 "{Binding PropertyHoldingMyValue}"

标签 c# silverlight data-binding dependency-properties

我正在创建一个自定义的“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}" />

现在解决问题:当我创建控件时,绑定(bind)它只能做到这一点:

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

最佳答案

如果我理解正确的话,您正在尝试将控件的 XAML 标记中的元素的属性绑定(bind)到控件本身的属性。

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

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();
    }
}

关于c# - 绑定(bind)到 DependencyProperty 仅适用于 "MyValue",不适用于 "{Binding PropertyHoldingMyValue}",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12319374/

相关文章:

c# - 从后面的代码创建到 Resources.resx 中字符串的绑定(bind)

c# - 将 html 字符串中的一个类替换为另一个类

vb.net - 使用 File.WriteAllBytes(filePath, byteData) 生成的 pdf 中出现错误

silverlight - 在哪里可以找到 Silverlight 4 的 F# 运行时

javascript - Knockout.js 条件绑定(bind)

c# - 模拟的 SignInManager 未按预期工作

c# - c# 中是否有 jQuery 扩展?

windows - 我们如何禁用 silverlight windows phone 7 中文本框的默认键盘?

angular - @输入 : Two way data binding not working

c# - 将 DataGridComboBoxColumn 与模型绑定(bind)