简体   繁体   English

在 WPF 应用程序中使用 RelativeSource 绑定(不是 ElementName)访问前一个兄弟元素?

[英]Access a previous sibling element using RelativeSource binding (not ElementName) in WPF Application?

I am trying to access a property of a sibling element that comes directly BEFORE the target element.我正在尝试访问直接位于目标元素之前的同级元素的属性。 This is something I will have to duplicate several times in my app so I'd rather use something reproduceable (not elementName) if possible.这是我必须在我的应用程序中多次复制的东西,所以如果可能的话,我宁愿使用可重现的东西(不是 elementName)。

Any Ideas?有任何想法吗?

<Groupbox x:Name="GB1">

  <Checkbox x:Name="CB1" IsChecked="True"/>

  <TextBlock>
    <Style TargetType="TextBlock">
      <Style.Triggers>

        <Data.Trigger Binding RelativeSource={??? (I want this to access the Checkbox CB1 above), Path=IsChecked}" Value="True>
            <Setter Property="*Do a Thing if IsChecked=True*" Value="..."/>

      </Style.Triggers>
    </Style>
  </TextBlock>
</Groupbox>

Maybe this example will give you an idea.也许这个例子会给你一个想法。

<StackPanel>
<TextBlock x:Name="textBlock1" Text="Text" />
<TextBlock x:Name="textBlock2" Text="Text">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Setter Property="Text" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type StackPanel}}, Path=Children[0].Text}"/>
        </Style>
    </TextBlock.Style>
</TextBlock>
</StackPanel>

Path=Children[0].Text it will look for the first child element of this ancestor and access its Text property. Path=Children[0].Text它将查找此祖先的第一个子元素并访问其 Text 属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM