开发者

Setting content in an inherited (BasedOn) style

开发者 https://www.devze.com 2022-12-13 18:19 出处:网络
I have a base style, say: <Style x:Key=\"DefaultButtonStyle\" TargetType=\"{x:Type Button}\"> <Setter Property=\"Background\" Value=\"{StaticResource DefaultButtonBackground}\" />

I have a base style, say:

<Style x:Key="DefaultButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="{StaticResource DefaultButtonBackground}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}"> 
                <Grid x:Name="ButtonShapeGrid" Height="{TemplateBinding Height}" Width="{TemplateBinding Height}">
                    <!-- snip some shaping Xaml. -->

                    <ContentPresenter 
                        Grid.RowSpan="2"
                        VerticalAlignment="{TemplateBinding VerticalAlignment}" HorizontalAlignment="Center" />

                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Now, I have a need to create some styles that replace the content of the button with something fixed...like so (where CloseButtonGlyph is a path resource). This works fine:

<Style x:开发者_StackOverflow中文版Key="CloseButtonStyle" BasedOn="{StaticResource DefaultButtonStyle}" TargetType="{x:Type Button}">
    <Setter Property="Content" Value="{StaticResource CloseButtonGlyph}" />
</Style>

However, I also need to add arbitrary Xaml into the content, and this is where I have a problem. The following doesn't work (I get a design-time error stating that the content can't be derived from ContentElement or Visual) when the style is used in a WPF window or control:

<Style x:Key="WingDingStyle" BasedOn="{StaticResource DefaultButtonStyle}" TargetType="{x:Type Button}">
    <Setter Property="Content">
        <Setter.Value>
             <TextBlock FontFamily="WebDings" Text="3" FontSize="18" Foreground="White" />
        </Setter.Value>
    </Setter>
</Style>

I'm at a loss as what I need to do. Putting a TextBox in the content of a button in a window or control works fine, why not in a style?


sorry! last time couldn't get time to test once, This code works well

<Style TargetType="{x:Type Button}">
    <Style.Resources>
        <TextBlock x:Key="myContent" FontFamily="WebDings" Background="Red" Text="3" FontSize="18" Foreground="White" />
    </Style.Resources>
    <Setter Property="Content" Value="{DynamicResource myContent}"/>
</Style>

and changing the setter like this also work fine

<Setter Property="Content" Value="{Binding Source={StaticResource myContent}}"/>

Hope it will help you!!


try something like this:

<Style x:Key="WingDingStyle" BasedOn="{StaticResource DefaultButtonStyle}" TargetType="{x:Type Button}">
    <Style.Resources>
        <TextBlock x:Key="myContent" FontFamily="WebDings" Text="3" FontSize="18" Foreground="White" />
    </Style.Resources>
    <Setter Property="Content" Value={StaticResource myContent}/>
</Style>

Hope this helps!!


I think the problem is that the style is shared across multiple buttons, so WPF is trying to add the same TextBlock to each button (obviously a TextBlock can only have one UI element as its parent).

Try setting the style to be "not shared" by default, so a new TextBlock is created for each button by adding an x:Shared attribute:

<Style x:Key="WingDingStyle" x:Shared="False" ...>
0

精彩评论

暂无评论...
验证码 换一张
取 消