开发者

Foreground property behavior confusion

开发者 https://www.devze.com 2023-03-22 21:39 出处:网络
I have a custom control like this: public class CustomControl1 : Control { private StackPanel panel; static CustomControl1()

I have a custom control like this:

    public class CustomControl1 : Control
{
    private StackPanel panel;

    static CustomControl1()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
    }

    public override void OnApplyTemplate()
    {
        panel = (StackPanel)GetTemplateChild("root");
        panel.Children.Add(new TextBlock { Text = "TextBlock added in the OnApplyTemplate method" });

        base.OnApplyTemplate();
    }
}

and its control template is like this:

<Style TargetType="{x:Type local:CustomControl1}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                <StackPanel Name="root">
                    <TextBlock>TextBlock added in ControlTemplate</TextBlock>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

then I use it in the main window:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    xmlns:app1="clr-namespace:WpfApplication1">
<Grid>
    <Grid.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Foreground" Value="Green"></Setter>
        </Style>
    </Grid.Resources>

    <app1:CustomControl1 Foreground="Red">

    </app1:CustomControl1>
</Grid>

if I run it, it'll be like this:

Foreground property behavior confusion

So my confusion is that the TextBlock in ControlTemplate follows the local value of Foreground. But the TextBlock added in the OnApplyTemplate method follows the value from the style.

But what I want is a TextBlock that only follows the style when no local value is present.

So why do the two TextBlocks behave differently and how can I get a TextBlock that only follows the style when no local value is开发者_StackOverflow present?

Note: How can I make the TextBlocks inside of the custom control not affected by an implicit style in the Resources of the Grid(which contains the custom control).


When you apply local value for Foreground you are applying to the CustomControl, whereas in the style you are applying only to TextBlock that makes lot of difference. Get rid of Grid.Resources and move your style setter directly in ControlTemplate and it will work as expected.

<Style TargetType="{x:Type local:CustomControl1}">
    <Setter Property="Foreground" Value="Green"></Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                <StackPanel Name="root">
                    <TextBlock>TextBlock added in ControlTemplate</TextBlock>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
0

精彩评论

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