gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:17:11 27 4
gpt4 key购买 nike

我正在创建一个自定义的“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 中保存 header 值的属性:

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

主页.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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com