【问题标题】:Bind header textblock isEnabled to parent Groupbox isEnabled将标题文本块 isEnabled 绑定到父 Groupbox isEnabled
【发布时间】:2016-08-02 04:49:13
【问题描述】:

参考这个问题:WPF Databinding: How do I access the "parent" data context?

我想做一些类似的事情,但是对于 Groupbox 的标题(因为标题与 Box 被禁用无关,因此始终为黑色,而其余部分为浅灰色。如果全部的话,这对我来说有点奇怪盒子的内容是灰色的,上面是灰色的,但是盒子标题本身是黑色的。

所以我尝试使用 flq 的链接问题中提到的方法将标题文本块的 isEnabled 属性简单地绑定到 groupbox 的 isEnabled 属性,但似乎我的绑定在某些时候失败了,我不知道具体在哪里以及为什么。

这是我当前的代码:

<GroupBox Header="Change Steps" Grid.Row="2" Grid.ColumnSpan="3" Name="gbChangeSteps">
     <GroupBox.Style>
          <Style TargetType="GroupBox">
              <Setter Property="HeaderTemplate">
                  <Setter.Value>
                      <DataTemplate>
                          <TextBlock Text="{Binding}" FontWeight="Bold" Height="19" Foreground="Black" IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}, Path=isEnabled}"/>
                      </DataTemplate>
                  </Setter.Value>
              </Setter>
          </Style>
      </GroupBox.Style>
<!-- ... (some non relevant Content)-->
</GroupBox>

【问题讨论】:

    标签: wpf data-binding


    【解决方案1】:

    经过进一步研究,我找到了帖子Disable groupBox including the groupBox name in WPF 这导致我与 Properties->Create Databinding->Binding type->UIElement 结合使用解决了这两个问题的解决方案,这个问题是关于一个和导致整个重新设计的原始问题,即像小的字母g 在标题中搞砸了。 这是解决问题的代码:

    <GroupBox.Style>
       <Style TargetType="{x:Type GroupBox}">
           <Setter Property="HeaderTemplate">
              <Setter.Value>
                   <DataTemplate>
                       <TextBlock Text="{Binding}" FontWeight="Bold" Height="19" IsEnabled="{Binding IsEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UIElement}}}">
                           <TextBlock.Style>
                               <Style>
                                   <Style.Triggers>
                                       <Trigger Property="Control.IsEnabled" Value="False">
                                           <Setter Property="Control.Foreground" Value ="#FF6D6D6D" />
                                       </Trigger>
                                   </Style.Triggers>    
                               </Style>
                          </TextBlock.Style>
                       </TextBlock>
                   </DataTemplate>
               </Setter.Value>
           </Setter>
         </Style>
    </GroupBox.Style>
    

    【讨论】:

      猜你喜欢
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-05
      • 1970-01-01
      • 1970-01-01
      • 2013-08-04
      相关资源
      最近更新 更多