Wpf 样式:通过 ElementName 绑定到子属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14983258/
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
Wpf Style: Binding to Child Property via ElementName
提问by makim
I have an Application that gives me the Option to change the Application-Theme via a RessourceDictionary, meaning I can only use pure XAML.
我有一个应用程序,它为我提供了通过 RessourceDictionary 更改应用程序主题的选项,这意味着我只能使用纯 XAML。
Now, what I want to do is hide a DockPanel, that does not have an ElementName, but it has a Child with an x:Name Property.
现在,我想要做的是隐藏一个 DockPanel,它没有 ElementName,但它有一个带有 x:Name 属性的 Child。
<Style TargetType="DockPanel">
<Setter Property="Visibility" Value="{Binding RelativeSource={RelativeSource Self}, Path=Child.Visibility}"/>
</Style>
With this Code I′m getting the first Child of the DockPanel, but I want to bind the value to the specific Child which has a unique ElementName.
使用此代码,我将获得 DockPanel 的第一个 Child,但我想将该值绑定到具有唯一 ElementName 的特定 Child。
So not every DockPanel disappears, only the one that has a Child of Type e.g. "TextBox" with an Elementname of "MyTextBox".
因此,并非每个 DockPanel 都会消失,只有具有子类型的 DockPanel,例如“TextBox”,元素名称为“MyTextBox”。
Anybody has an Idea how to do that? Thanks ;-)
任何人都有一个想法如何做到这一点?谢谢 ;-)
回答by makim
Got it working the DockPanel has a Border as Parent and with this Code I can set the Visibility of this Border to Hidden!
让它工作 DockPanel 有一个边框作为父级,使用此代码我可以将此边框的可见性设置为隐藏!
<Style TargetType="Border">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Child.Children[2].Name}"
Value="SearchTextBox">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
Possible improvement: Do not reference the TextBox per Index, iterate through childs...
可能的改进:不要引用每个索引的 TextBox,遍历 childs...

