wpf - 在WPF中,有没有办法绑定(bind)到同级属性?

标签 wpf data-binding xaml

我有一系列 TextBlockTextBox 控件。有没有办法将 Style 应用于 TextBlock ,以便它们可以立即与它们之后的控件进行数据绑定(bind)?

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

<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}"/>

这样的事情可能吗?

最佳答案

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

这是我如何使用它的示例。我有一个 TextBox 控件,当它获得焦点时,它会以“突出显示颜色”亮起。此外,我希望当 TextBox 获得焦点时,其关联的 Label 控件也能亮起。因此,我为 Label 控件编写了一个触发器,使其以与 TextBox 控件类似的方式点亮。此触发器由名为 IsFocusedByProxy 的自定义附加属性触发。然后我需要将 Label 的 IsFocusedByProxy 绑定(bind)到 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 更好。 但不同之处在于,现在我可以将此绑定(bind)移动到样式中以实现可重用性:

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

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

<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 ... -->

这给了我这些结果:

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

Before any TextBoxes have focus

不同的文本框接收焦点:

after focus 1

after focus 2

关于wpf - 在WPF中,有没有办法绑定(bind)到同级属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/870931/

相关文章:

c# - 在 WPF 中的用户控件上创建可绑定(bind)属性的正确方法是什么?

C# Comport - 可能丢失数据

c# - 将 DataTemplate 中的按钮绑定(bind)到表单的 ViewModel 中的命令

c# - 避免可重用 WPF 控件中的 XAML 资源键冲突

c# - WPF 选项卡顺序工作错误

c# - WPF TabItem HeaderTemplate

c# - 如何以编程方式将运行分配给文本属性?

c# - ExcelWorksheet 维度 NullReferenceException

data-binding - 从子元素访问 knockout 绑定(bind)

wpf - 使用 WPF 显式绑定(bind)时检查挂起的更改