gpt4 book ai didi

wpf - 如何绑定(bind)到 RelativeSource Self?

转载 作者:行者123 更新时间:2023-12-03 13:05:46 30 4
gpt4 key购买 nike

我试图在我的 Xaml 中绑定(bind)几个不同的属性:

<Label Content="{Binding Description}" 
Visibility="{Binding Path=DescriptionVisibility,
ElementName=_UserInputOutput}"
FontSize="{Binding Path=FontSizeValue, ElementName=_UserInputOutput}"
HorizontalAlignment="Left" VerticalAlignment="Top" Padding="0" />

你会注意到我在这里使用了两种不同的绑定(bind)技术。使用元素名称的工作,另一个不工作。这是后面的代码:
public string Description
{
get { return (string)GetValue(DescriptionProperty); }
set { SetValue(DescriptionProperty, value); }
}
public static readonly DependencyProperty DescriptionProperty =
DependencyProperty.Register("Description", typeof(string), typeof(UserControl),
new UIPropertyMetadata(""));

每个绑定(bind)都有不同的名称,但它们大部分看起来都像这样。
我希望我的 Binding 能够使用:
{Binding Description}

代替:
{Binding Path=Description, ElementName=_UserInputOutput}

它似乎只在使用 ElementName 时才有效。我需要导出/导入这个 XAML,所以我不能有 ElementName,否则导入将不起作用。

我认为这是最好的:
{Binding Path=Description, RelativeSource={RelativeSource Self}}

这没有用。

有任何想法吗??谢谢!

最佳答案

{RelativeSource Self}如果您在 Label 上有这样的绑定(bind),则以拥有正在绑定(bind)的属性的对象为目标。它将寻找 Label.Description ,这是不存在的。相反,您应该使用 {RelativeSource AncestorType=UserControl} .

没有源的绑定(bind)( ElementNameSourceRelativeSource )是相对于 DataContext , 但是在 UserControls你应该 avoid setting the DataContext 不要弄乱外部绑定(bind)。

关于wpf - 如何绑定(bind)到 RelativeSource Self?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11995318/

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