views:

78

answers:

2

I have a GroupBox, which is defined like this

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Groupbox"
    >
    <Style TargetType="local:GroupBox">
        <Setter Property="BorderBrush" Value="DarkGray"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Padding" Value="6"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:GroupBox">
                    <Grid Background="{TemplateBinding Background}">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Border BorderThickness="{TemplateBinding BorderThickness}" Grid.Row="1" Grid.RowSpan="2" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="3">
                            <Border.Clip>
                                <GeometryGroup FillRule="EvenOdd">
                                    <RectangleGeometry x:Name="FullRect" Rect="0,0,300,200"/>
                                    <RectangleGeometry x:Name="HeaderRect" Rect="6,0,100,100"/>
                                </GeometryGroup>
                            </Border.Clip>
                        </Border>
                        <ContentPresenter Grid.Row="2" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Margin="{TemplateBinding Padding}"/>
                        <ContentControl x:Name="HeaderContainer" Margin="6,0,0,0" Grid.Row="0" Grid.RowSpan="2" HorizontalAlignment="Left">
                            <ContentPresenter Margin="3,0,3,0" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}"/>
                        </ContentControl>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

And i'm using this control like this

<Controls:GroupBox x:Name="GroupBox">
   <Controls:GroupBox.HeaderTemplate>
     <DataTemplate>
        <CheckBox "Header" x:Name="cbHeader"/>
     </DataTemplate>
   </Controls:GroupBox.HeaderTemplate>
<Controls:GroupBox>

So, well, my questions - how can i bind property IsEnabled of GroupBox to the Checkbox property IsChecked? Thanks in advance.

A: 

Didn't try it but you should be able to do something like this:

<Controls:GroupBox x:Name="MyGroupBox">
   <Controls:GroupBox.HeaderTemplate>
     <DataTemplate>
        <CheckBox IsChecked="{Binding Path=IsEnabled, ElementName=MyGroupBox, Mode=TwoWay}" Content="Header" x:Name="cbHeader" />
     </DataTemplate>
   </Controls:GroupBox.HeaderTemplate>
<Controls:GroupBox>
David
Have you tested this, the `TemplatedParent` in this case is would seem to be the ContentPresenter in from the Control template.
AnthonyWJones
Didn't test it, no. My edit worked for me with an ItemsControl instead of a GroupBox but the concept is the same.
David
Your sample binds IsChecked to IsEnabled, and i need to bind it vice versa - IsEnabled to IsChecked.
Walkor
@walkor: does it really matter which way round a two way binding is?
AnthonyWJones
oh, i didn'n notice it's two-way. I'll check it later, thanks
Walkor
This helped, thanks.
Walkor
A: 

In fact there was no need to define DataTemplate. This code works the same way.

<Panels:GroupBox>
  <Panels:GroupBox.Header>
    <CheckBox Content="Header" x:Name="cbHeader" IsChecked="{Binding IsHeaderChecked}" />
  </Panels:GroupBox.Header>
  <ContentControl HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" IsEnabled="{Binding Path=IsChecked, ElementName=cbHeader}"/>
</Panels:GroupBox>
Walkor