绑定不适用于GroupBox.HeaderTemplate

时间:2014-07-25 10:15:02

标签: c# wpf xaml header groupbox

我为GroupBox创建了一个自定义标题,如下图所示:

Custom GroupBox

我还要在此标题的Backgroud上设置Binding,所以我编写了以下代码:

 <GroupBox Width="130"
                      Height="80"
                      BorderBrush="Black"
                      Margin="5"
                      Grid.Row="0"
                      Background="{Binding HColor}">
                <GroupBox.HeaderTemplate>
                    <DataTemplate>
                        <Border BorderBrush="Black"
                                BorderThickness="1"
                                CornerRadius="3"
                                HorizontalAlignment="Stretch"
                                Width="70"
                                Margin="-2,0,-3,-1"
                                Height="20"
                                Background="{Binding HColor}">
                            <TextBlock Text="Hubs"
                                       Foreground="Black"
                                       FontWeight="DemiBold"
                                       HorizontalAlignment="Center" />
                        </Border>
                    </DataTemplate>
                </GroupBox.HeaderTemplate>
 </GroupBox>

执行后,GroupBox的背景是正确的colord,但不是标题!

Custom GroupBox

有人能够向我解释为什么它不能正常运作吗?

4 个答案:

答案 0 :(得分:3)

你可以这样使用;

    <GroupBox Width="130"
                  Height="80"
                  BorderBrush="Black"
                  Margin="5"
                  Grid.Row="0"
                  Background="{Binding HColor}">
        <GroupBox.Header>
            <Border BorderBrush="Black"
                            BorderThickness="1"
                            CornerRadius="3"
                            HorizontalAlignment="Stretch"
                            Width="70"
                            Margin="-2,0,-3,-1"
                            Height="20"
                            Background="{Binding Path=HColor}">
                <TextBlock Text="Hubs"
                                   Foreground="Black"
                                   FontWeight="DemiBold"
                                   HorizontalAlignment="Center" />
            </Border>

        </GroupBox.Header>

        <!--<DataTemplate>
                <Border BorderBrush="Black"
                            BorderThickness="1"
                            CornerRadius="3"
                            HorizontalAlignment="Stretch"
                            Width="70"
                            Margin="-2,0,-3,-1"
                            Height="20"
                            Background="{Binding Path=HColor}">
                    <TextBlock Text="Hubs"
                                   Foreground="Black"
                                   FontWeight="DemiBold"
                                   HorizontalAlignment="Center" />
                </Border>
            </DataTemplate>-->

    </GroupBox>

答案 1 :(得分:1)

找到解决方案!

我必须在Xaml中进行内部绑定,在GroupBox的背景和它的标题之间。像下面的代码一样:

  <GroupBox Name="HubGroupBox"
                      Width="130"
                      Height="80"
                      BorderBrush="Black"
                      Margin="5"
                      Grid.Row="0"
                      Background="{Binding HubColor}">
                <GroupBox.HeaderTemplate>
                    <DataTemplate>
                        <Border Canvas.ZIndex="2"
                                BorderBrush="Black"
                                BorderThickness="1"
                                CornerRadius="3"
                                HorizontalAlignment="Stretch"
                                Width="70"
                                Margin="-2,0,-3,-1"
                                Height="20"
                                Background="{Binding ElementName=HubGroupBox, Path=Background}">
                            <TextBlock Text="Hubs"
                                       Foreground="Black"
                                       FontWeight="DemiBold"
                                       HorizontalAlignment="Center" />
                        </Border>
                    </DataTemplate>
                </GroupBox.HeaderTemplate>
  </GroupBox>

结果是:

enter image description here

答案 2 :(得分:0)

您可以使用此绑定而不使用元素名称

<GroupBox Width="130"
                  Height="80"
                  BorderBrush="Black"
                  Margin="5"
                  Grid.Row="0"
                  Background="{Binding HColor}">
        <GroupBox.HeaderTemplate>
            <DataTemplate>
                <Border BorderBrush="Black"
                            BorderThickness="1"
                            CornerRadius="3"
                            HorizontalAlignment="Stretch"
                            Width="70"
                            Margin="-2,0,-3,-1"
                            Height="20" Background="{Binding Background, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}}">
                    <TextBlock Text="Hubs"
                                   Foreground="Black"
                                   FontWeight="DemiBold"
                                   HorizontalAlignment="Center" />
                </Border>
            </DataTemplate>
        </GroupBox.HeaderTemplate>
    </GroupBox>

答案 3 :(得分:0)

您可以依赖VM数据而不是控件属性

<GroupBox Name="HubGroupBox"
                  Width="130"
                  Height="80"
                  BorderBrush="Black"
                  Margin="5"
                  Grid.Row="0"
                  DataSource="{Binding VMObject}">
            <GroupBox.HeaderTemplate>
                <DataTemplate>
                    <Border Canvas.ZIndex="2"
                            BorderBrush="Black"
                            BorderThickness="1"
                            CornerRadius="3"
                            HorizontalAlignment="Stretch"
                            Width="70"
                            Margin="-2,0,-3,-1"
                            Height="20"
                            Background="{Binding ElementName=HubGroupBox, Path=DataSource.HColor}">
                        <TextBlock Text="Hubs"
                                   Foreground="Black"
                                   FontWeight="DemiBold"
                                   HorizontalAlignment="Center" />
                    </Border>
                </DataTemplate>
            </GroupBox.HeaderTemplate>