4

我刚刚在标题中创建了一个带有复选框的 Expander 样式:

<Style TargetType="{x:Type Expander}" x:Key="MyCheckboxExpander">
    <Setter Property="Background" Value="{StaticResource ExpanderBackgroundBrush}"/>
    <Setter Property="HeaderTemplate" Value="{StaticResource MyHeaderExpander}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Expander}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*" x:Name="ContentRow"/>
                    </Grid.RowDefinitions>
                    <Border Grid.Row="0" x:Name="Border" Background="{TemplateBinding Background}" BorderThickness="1" 
                            BorderBrush="{StaticResource ExpanderBorderBrush}" CornerRadius="2,2,0,0">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="18"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="20"/>
                            </Grid.ColumnDefinitions>
                            <CheckBox VerticalAlignment="Center" Margin="4,0,0,2"/>
                            <ToggleButton Grid.Column="2" x:Name="ToggleBt" Template="{DynamicResource MyExpanderToggleButton}" Background="{StaticResource ExpanderBackgroundBrush}" 
                                          IsChecked="{Binding Path=IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" 
                                          OverridesDefaultStyle="True"/>
                            <ContentPresenter Grid.Column="1" Margin="0,0,0,0" RecognizesAccessKey="True" ContentSource="Header"/>
                        </Grid>
                    </Border>
                    <Border Visibility="Collapsed" Grid.Row="1" x:Name="ExpandSite" Background="{StaticResource ContentBackgroundBrush}"
                            BorderBrush="{StaticResource ExpanderBorderBrush}" BorderThickness="1,0,1,1" CornerRadius="0,0,2,2">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" 
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Focusable="false"/>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsExpanded" Value="True">
                        <Setter Property="Visibility" Value="Visible" TargetName="ExpandSite"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Background" Value="{StaticResource ExpanderBackgroundBrush}" TargetName="Border"/>
                        <Setter Property="BorderBrush" Value="{StaticResource ExpanderBackgroundBrush}" TargetName="Border"/>
                        <Setter Property="Visibility" Value="Hidden" TargetName="ToggleBt" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

如何为此扩展器创建新属性 (IsChecked) 以绑定到布尔值?e. G:

<Expander IsChecked="{Binding MyBoolValue}" />

同样在模板上:

<CheckBox VerticalAlignment="Center" Margin="4,0,0,2" IsChecked="{TemplateBinding ????}" />

编辑:

我发现了一个类似的问题,但我想知道是否有另一种方法可以在不使用控制继承的情况下做到这一点。

4

1 回答 1

3

我创建了一个这样的附加属性:

public sealed class AP : DependencyObject 
{
    public static bool GetIsChecked(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsCheckedProperty);
    }

    public static void SetIsChecked(DependencyObject obj, bool value)
    {
        obj.SetValue(IsCheckedProperty, value);
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IsCheckedProperty =
        DependencyProperty.RegisterAttached("IsChecked", typeof(bool), typeof(AP), new UIPropertyMetadata(false));
}

然后我所要做的就是:

<Expander my:AP.IsChecked="{Binding Path=TestBool}" />   

在模板上:

<CheckBox VerticalAlignment="Center" Margin="4,0,0,2" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(my:AP.IsChecked)}" />

我想知道如果样式是在不同的组件中定义的,该怎么办...

于 2012-07-11T15:29:06.143 回答