Как наследовать стили на основе типов в WPF? - PullRequest
0 голосов
/ 16 апреля 2009

Я хочу использовать один и тот же стиль для всех Image с и AutoGreyableImage с (мой пользовательский элемент управления, наследуемый от Image). У меня есть следующий стиль, объявленный для всего приложения:

<Style TargetType="{x:Type Image}"
    x:Key="ImageType">
    <Setter Property="Stretch"
            Value="Uniform" />
    <Setter Property="Height"
            Value="16" />
    <Setter Property="Width"
            Value="16" />
    <Setter Property="SnapsToDevicePixels"
            Value="True" />
</Style>

Но AutoGreyableImage не принимают стиль. Это тоже не работает:

<Style TargetType="{x:Type my:AutoGreyableImage}"
       BasedOn="{DynamicResource ImageType}" />

Как правильно это сделать?

Ответы [ 2 ]

3 голосов
/ 16 апреля 2009

Вы должны использовать ссылку StaticResource в зависимом стиле.

Попробуйте это:

<Style TargetType="{x:Type my:AutoGreyableImage}" 
       BasedOn="{StaticResource ImageType}" />
3 голосов
/ 16 апреля 2009

У меня отлично работает.

AutoGreyableImage.cs

public class AutoGreyableImage : Image
{
    public static readonly DependencyProperty CustomProperty = DependencyProperty.Register("Custom",
        typeof(string),
        typeof(AutoGreyableImage));

    public string Custom
    {
        get { return GetValue(CustomProperty) as string; }
        set { SetValue(CustomProperty, value); }
    }
}

Window.xaml

<Window.Resources>
    <Style TargetType="Image" x:Key="ImageStyle">
        <Setter Property="Stretch" Value="Uniform"/>
    </Style>

    <Style TargetType="{x:Type local:AutoGreyableImage}" BasedOn="{StaticResource ImageStyle}">
        <Setter Property="Custom" Value="Hello"/>
        <Setter Property="Width" Value="30"/>
    </Style>
</Window.Resources>
<Grid>
    <local:AutoGreyableImage Source="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Winter.jpg"/>
</Grid>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...