Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 绑定在GroupBox.HeaderTemplate上不起作用_C#_Wpf_Xaml_Header_Groupbox - Fatal编程技术网

C# 绑定在GroupBox.HeaderTemplate上不起作用

C# 绑定在GroupBox.HeaderTemplate上不起作用,c#,wpf,xaml,header,groupbox,C#,Wpf,Xaml,Header,Groupbox,我为我的GroupBox创建了一个自定义标题,如下图所示: 我还想在此标头的背景上设置绑定,因此我编写了以下代码: <GroupBox Width="130" Height="80" BorderBrush="Black" Margin="5" Grid.Row="0"

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

我还想在此标头的背景上设置绑定,因此我编写了以下代码:

 <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的背景进行着色,但不是标题


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

你可以这样使用

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

你可以这样使用

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

找到解决方案

我必须在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>

结果是:


找到解决方案

我必须在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>

结果是:


您可以在不使用元素名称的情况下使用此绑定

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

您可以在不使用元素名称的情况下使用此绑定

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

您可以依赖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>


您可以依赖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>


它不是那样工作的!我以前试过,标题是一个描述边界的字符串!而不是它的观点。谢天谢地,事实上,我已经试过了,而且效果很好。我有一个形象,但他们不允许我把形象,因为低声誉,它不是这样的工作!我以前试过,标题是一个描述边界的字符串!而不是它的观点。谢天谢地,事实上,我已经试过了,而且效果很好。我有一个形象,但他们不允许我把形象,因为低声誉