wpf 我如何绑定到RelativeSource Self?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11995318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:29:25  来源:igfitidea点击:

How do I bind to RelativeSource Self?

wpfbinding

提问by B-Rad

I am trying to bind several different properties in my Xaml:

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

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

You will noticed I have used two Different binding techniques here. The ones using Element Name work, the other does not. Here is code behind:

您会注意到我在这里使用了两种不同的绑定技术。使用 Element Name 的那些工作,其他没有。这是后面的代码:

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

Each Binding has a different name but they all look like this for the most part. I want my Binding to be able to work with:

每个 Binding 都有不同的名称,但大多数情况下它们都是这样的。我希望我的 Binding 能够使用:

{Binding Description}

Instead of:

代替:

{Binding Path=Description, ElementName=_UserInputOutput}

It only seems to be working when ElementName is used. I need to export/import this XAML, so I can't have a ElementName or the import won't work.

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

I thought this would be best:

我认为这将是最好的:

{Binding Path=Description, RelativeSource={RelativeSource Self}}

This did not work.

这没有用。

Any ideas?? Thank you!

有任何想法吗??谢谢!

回答by H.B.

{RelativeSource Self}targets the object that owns the property that is being bound, if you have such a binding on a Labelit will look for Label.Description, which isn't there. Instead you should use {RelativeSource AncestorType=UserControl}.

{RelativeSource Self}目标是拥有被绑定的属性的对象,如果你有这样的绑定,Label它会寻找Label.Description,它不存在。相反,您应该使用{RelativeSource AncestorType=UserControl}.

Bindings without a source (ElementName, Source, RelativeSource) are relative to the DataContext, however in UserControlsyou should avoidsetting the DataContextto not mess with external bindings.

没有源 ( ElementName, Source, RelativeSource) 的绑定是相对于 的DataContext,但是UserControls您应该避免将 设置DataContext为不与外部绑定混淆。

回答by Joel Lucsy

You haven't set the DataContext, which is what the RelativeSource is using to determine what it's relative to. You need to set the DataContext at a higher level, like the UserControl. I typically have:

您还没有设置 DataContext,它是 RelativeSource 用来确定它相对于什么的。您需要在更高级别设置 DataContext,例如 UserControl。我通常有:

<UserControl ... DataContext="{Binding RelativeSource={RelativeSource Self}}">
</UserControl>

This tells the UserControl to bind itself the class in the codebehind.

这告诉 UserControl 将自己绑定到代码隐藏中的类。