2014-07-25 5 views
0
작동하지

나는 다음과 같은 이미지처럼 내 그룹 상자에 대한 사용자 지정 헤더를 만들어 :이 GroupBox.HeaderTemplate

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

실행 후, 나는이 그룹 상자의 배경이 올바른 방법으로 colord하지만, 하지 헤더!

Custom GroupBox

는 잘 작동하지 않는 이유를 할 사람이 나에게 설명 할 수 있나요?

답변

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

1

그런 식으로 사용할 수 있습니다.

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

그런 식으로 작동하지 않습니다! 나는 전에 시험해 보았다. 그리고 헤더는 경계를 기술하고있는 끈이었다! 보기가 아닙니다. 감사합니다. @Ugur – NTinkicht

+0

사실, 나는 노력했고 작동 중입니다. 나는 이미지를 가지고 있지만 저의 평판이 좋지 않아 이미지를 넣지 못하게합니다. – Ugur

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

당신은 당신의 VM 데이터 대신 제어에 의존 할 수이 요소 이름없이 바인딩을 사용할 수있는 followiing 코드처럼 속성

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