C# 对DependencyProperty的绑定仅适用于;“我的价值”;而不是",;{Binding PropertyHoldingMyValue}";
我正在创建一个自定义的“PageHeaderControl”用户控件,其标题属性为:C# 对DependencyProperty的绑定仅适用于;“我的价值”;而不是",;{Binding PropertyHoldingMyValue}";,c#,silverlight,data-binding,dependency-properties,C#,Silverlight,Data Binding,Dependency Properties,我正在创建一个自定义的“PageHeaderControl”用户控件,其标题属性为: public partial class PageHeaderControl: UserControl { public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(string), ty
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}" />
我想也许我的房子搞砸了,但这也行得通:
<TextBlock Text="{Binding PageHeader,Mode=TwoWay}" />
编辑2:
当我在我的
PageHeader
属性的“get”中放置断点时,它根本不会被命中,除非我在TextBlock中添加…我有点困惑,我认为您错过了绑定内联表达式的语法
在“{Binding”之后是指向您的属性的路径。“PageHeader”是指向您的属性的路径吗
我想你的意思是:
<my:PageHeader Header="{Binding PageHeader, Mode=TwoWay}" />
<TextBlock Text="{Binding PageHeader, Mode=TwoWay}" />
干杯如果我理解正确,您正在尝试将控件的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>
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>
根据这个问题,在
TextBlock
中绑定到PageHeader
确实有效。我假设OP的视图模型中有一个名为PageHeader
的属性,它包含要在my:PageHeader
控件(?)中显示的文本PageHeader是指向我的ViewModel中属性的路径,抱歉,我没有说得更清楚。最好在ViewModel中包含PageHeader属性的定义。但是DependencyProperty在我的控件中,而不是在我的视图模型中…没有办法将DPs排除在视图模型之外吗?如果双向绑定的目标对象是您的模型,那么我将t应该实现上述接口或成为DependencyObject。但是,如果双向绑定的目标对象是控件,则应该更改绑定表达式并将其绑定到控件的属性。我认为您的问题在于模型问题,而不是技术问题。欢迎尝试使用HeaderedContent控制?这就是它的目的
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); }
}
<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>
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();
}
}
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;
}
<Grid x:Name="LayoutRoot" Background="White">
<my:PageHeaderControl Header="{Binding PageHeader, Mode=TwoWay}"></my:PageHeaderControl>
</Grid>
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
DataContext = new ViewModel();
}
}