【问题标题】:In WPF, is there a way to bind to sibling properties?在 WPF 中,有没有办法绑定到兄弟属性?
【发布时间】:2009-05-15 21:23:21
【问题描述】:

我有一系列TextBlockTextBox 控件。有没有办法将Style 应用于TextBlocks,以便它们可以在它们之后立即数据绑定到控件?

我希望能够做这样的事情:

<Resources..>
    <Style x:Key="BindToFollowingTextBoxSibling">
        <Setter Property="TextBlock.Text" Value="{Binding RelativeSource={RelativeSource FollowingSibling}, Path=Text, Converter={StaticResource MyConverter}}" />
        <Setter Property="TextBlock.Background" Value="{Binding RelativeSource={RelativeSource FollowingSibling}, Path=Text, Converter={StaticResource TextToBrushConverter}}" />
        ... More properties and converters.
    </Style>
</Resources>

...

<TextBlock Style="{StaticResource BindToFollowingTextBoxSibling}"/>
<TextBox/>

<TextBlock Style="{StaticResource BindToFollowingTextBoxSibling}"/>
<TextBox/>
<TextBlock Style="{StaticResource BindToPreviousTextBoxSibling}"/>

这样的事情可能吗?

【问题讨论】:

    标签: wpf data-binding xaml


    【解决方案1】:

    我知道这是一个旧线程,但我找到了解决此问题的方法。我能够使用 Aland Li 的建议,找到here。它不像在 CSS 中那样通用,但如果您知道父元素类型,这即使在样式中也能很好地工作

    这是我如何使用它的示例。我有一个 TextBox 控件,当它具有焦点时会以“突出显示颜色”点亮。此外,我希望其关联的 Label 控件在 TextBox 获得焦点时也会亮起。因此,我为 Label 控件编写了一个 Trigger,使其以与 TextBox 控件类似的方式点亮。此触发器由名为 IsFocusedByProxy 的自定义附加属性触发。然后我需要将标签的 IsFocusedByProxy 绑定到 TextBox 的 IsFocused。所以我使用了这种技术:

    <Grid x:Name="MaxGrid">
        <Label  x:Name="MaxLabel"
                Content="Max:"  
                c5:TagHelper.IsFocusedByProxy="{Binding 
                                      Path=Children[1].IsFocused,
                                      RelativeSource={RelativeSource AncestorType=Grid}}" 
           />
         <c5:TextBoxC5Mediator x:Name="MaxTextBox"                          
                               DataContext="{Binding ConfigVm.Max_mediator}" />
    </Grid>
    

    此时您可能会想,这并不比仅在 Binding 中使用 ElementName 更好。 但不同的是,现在我可以将此绑定移动到 Style 中以实现可重用性:

    <Setter Property="C5_Behaviors:TagHelper.IsFocusedByProxy"
            Value="{Binding Path=Children[1].IsFocused,
                         RelativeSource={RelativeSource AncestorType=Grid}}" />
    

    现在,当我有一个充满这些事件的视图时,我可以像这样(我已经设置了要隐式应用的必要样式,这就是为什么没有显示设置样式的标记的原因):

    <Grid x:Name="MaxGrid">
        <Label  x:Name="MaxLabel"
                Content="Max:"  />
        <c5:TextBoxC5Mediator x:Name="MaxTextBox"                          
                              DataContext="{Binding ConfigVm.Max_mediator}" />
     </Grid> 
     <Grid x:Name="MinGrid">
         <Label  x:Name="MinLabel"
                 Content="Min:" />
         <c5:TextBoxC5Mediator x:Name="MinTextBox"                          
                               DataContext="{Binding ConfigVm.Min_mediator}" />
    </Grid>
    <Grid x:Name="StepFactorGrid">
        <Label  x:Name="StepFactorLabel"
                Content="Step Factor:" />
        <c5:TextBoxC5Mediator x:Name="StepFactorTextBox"                          
                              DataContext="{Binding ConfigVm.StepFactor_mediator}" />
    </Grid>
    <!-- ... and lots more ... -->
    

    这给了我这些结果

    在任何文本框获得焦点之前:

    不同的文本框接收焦点:

    【讨论】:

    • {Binding Path=Children[1].IsFocused, RelativeSource={RelativeSource AncestorType=Grid}}+1。
    • 所以我假设 Path=Children[1]... 中的“1”是索引?这确实对我有用,但想澄清数字值的含义。
    • @EdwardM 正确,Path=Children[1] 中的 1 是一个索引(当然是从零开始的索引)。因此,如果您的结构不同,您可以调整该数字。干杯。
    【解决方案2】:

    我认为在这种情况下最好的做法是通过 ElementName 绑定:

    <TextBlock Text="{Binding ElementName=textBox1, Path=Text}" />
    <TextBox x:Name="textBox1">this is the textBox's 1 text</TextBox>
    <TextBlock Text="{Binding ElementName=textBox2, Path=Text}" />
    <TextBox x:Name="textBox2">this is the textBox's 2 text</TextBox>
    

    它会达到类似的效果。这对你有用吗?

    【讨论】:

    • 我可能会这样做。问题是我正在考虑绑定几个属性和转换器,因此为每个属性执行此操作需要大量复制和过去。我在想我可能会通过 ElementName 绑定到 TextBlock 的标签,然后使用 RelativeSource Self 来获取我需要的属性。
    • 我最终只是创建了一个用户控件来处理我需要的情况。这更容易了。
    • 哈哈帅哥,我的解决方案不是解决您问题的最佳方法,很高兴您解决了它 =)
    猜你喜欢
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    相关资源
    最近更新 更多