質問

私は、WPFエキスパンダーヘッダーにスタイルを適用したいと思います。以下のXAMLで、私はパンダを持っていますが、スタイルは、それのすべてのためだけではなく、ヘッダーのためです。

感謝します。

<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="640"
>
    <StackPanel>
        <StackPanel.Resources>
            <Style TargetType="Expander">
                <Style.Resources>
                    <LinearGradientBrush x:Key="BackBrush" StartPoint="0.5,0" EndPoint="0.5,1">
                        <GradientStop Color="#EF3132" Offset="0.1" />
                        <GradientStop Color="#D62B2B" Offset="0.9" />
                    </LinearGradientBrush>
                </Style.Resources>
                <Setter Property="Background" Value="{StaticResource BackBrush}"/>
            </Style>
        </StackPanel.Resources>
        <Expander>
            <StackPanel>
                <TextBlock>Bike</TextBlock>
                <TextBlock>Car</TextBlock>
                <TextBlock>Truck</TextBlock>
            </StackPanel>
        </Expander>
    </StackPanel>
</Page>
役に立ちましたか?

解決

私はからいくつかのXAMLを組み合わせていますスミス MSDNするそして解決策を考え出しました。実際、対照(AL少なくともヘッダ)がretemplatedする必要があります。

<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400">
    <StackPanel>
        <StackPanel.Resources>

            <Style TargetType="Border" x:Key="RacePitBorderStyle" >
                <Style.Resources>
                    <LinearGradientBrush x:Key="BackBrush" StartPoint="0.5,0" EndPoint="0.5,1">
                        <GradientStop Color="#EF3132" Offset="0.1" />
                        <GradientStop Color="#D62B2B" Offset="0.9" />
                    </LinearGradientBrush>
                </Style.Resources>
                <Setter Property="Background" Value="{StaticResource BackBrush}"/>
            </Style>

            <DataTemplate x:Key="titleText">
                <Border Style="{StaticResource RacePitBorderStyle}" Height="24">
                    <TextBlock Text="{Binding}" 
                        Margin="4 0"
                        VerticalAlignment="Center"
                        Foreground="White"
                        FontSize="11" 
                        FontWeight="Normal"
                        Width="{Binding
                        RelativeSource={RelativeSource
                        Mode=FindAncestor,
                        AncestorType={x:Type Expander}},
                        Path=ActualWidth}"
                        TextWrapping="Wrap"/>
                </Border>
            </DataTemplate>

            <Style TargetType="{x:Type Expander}">
                <Setter Property="HeaderTemplate" Value="{StaticResource titleText}"/>
            </Style>

        </StackPanel.Resources>

        <Expander Name="hcontCtrl" Header="This is the header.">
            <StackPanel>
                <TextBox>This is a textbox</TextBox>
                <Button>A button</Button>
            </StackPanel>
        </Expander>

    </StackPanel>
</Page>

他のヒント

私はバシレの答えは正しい軌道に乗っていると思うが、それは必要な元のポスターよりも多くないように思えます。すべての元の質問が何を求めていたことは、ヘッダーの背景を変更することでした。提示の変化はそれを行うんが、それはまた、他のことを行います。

これらの他のものの一つは、デフォルトの実装を置き換えることで、私はのTextBlockと、のContentPresenterを信じています。後で、我々はこののStackPanelに2つ目のエキスパンダーを追加するときに何が起こりますか?たぶん何かます:

<Expander>
    <Expander.Header>
        <StackPanel>
            <Border height="5" width="5" Foreground="Blue"/>
            <TextBlock>Ha!</TextBlock>
        </StackPanel>
    </Expander.Header>
</Expander>

私は知りませんが、それは良いではありません。代わりに、私たちはこの単純なを維持したいと考えています。

<DataTemplate x:Key="expanderHeader">
    <ContentPresenter
        Content={Binding}
        TextBlock.Background={StaticResource myBrush}/>
</DataTemplate>

<Style TargetType="Expander">
    <Setter Property="HeaderTemplate" Value="{StaticResource expanderHeader}"/>
</Style>
誰かが私たちのスタイルエキスパンダーでテキストだけではない何かを入れたときに

こうすることで、我々は壊れません。あなたは、彼らはおそらく希望されるこのような背景、と何の全体を包むことを確認したい場合は、それは次のようになります:

<DataTemplate x:Key="expanderHeader">
    <Border Background={StaticResource myBrush}>
        <ContentPresenter Content={Binding}/>
    </Border>
</DataTemplate>

あなたのスタイルにしたいものを依存 - あなたはそれのどの部分のスタイルを設定することができます。あなたは、ヘッダーの内容を変更したい場合は、単にExpander.HeaderプロパティにすべてのあなたのUIを置き、それがヘッダ領域に表示されます。

それはあなたのニーズを満たすdoes't場合は、

は、おそらくコントロールをテンプレートし直す必要があります。 に出荷されたコントロールテンプレートを見てみましょう>

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top