Почему мой пользовательский элемент управления не работает правильно? - PullRequest
0 голосов
/ 13 мая 2018

Я создаю пользовательский элемент управления для пользовательской кнопки, вот ее класс:

public class SButton : Button
    {
        static SButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SButton), new FrameworkPropertyMetadata(typeof(SButton)));
        }

        public Brush PressBackground
        {
            get { return (Brush)GetValue(PressBackgroundProperty); }
            set { SetValue(PressBackgroundProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PressBackground.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PressBackgroundProperty =
            DependencyProperty.Register("PressBackground", typeof(Brush), typeof(SButton), null);


    }

А вот Generic.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SealBase">
    <Style TargetType="{x:Type local:SButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:SButton}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" VerticalAlignment="{TemplateBinding VerticalAlignment}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" Background="{TemplateBinding Background}" Name="B">
                        <TextBlock FontSize="{TemplateBinding FontSize}" Foreground="{TemplateBinding Foreground}" FontFamily="{TemplateBinding FontFamily}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}">
                        <ContentPresenter></ContentPresenter>
                        </TextBlock>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter Property="Background" TargetName="B" Value="{Binding PressBackground, RelativeSource={RelativeSource Self}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

Когда я использовал SButton в XAML следующим образом:

<CustomControl:SButton PressBackground="Black" Background="Red" Content="555" Foreground="White"/>

Пользовательский элемент управления работал без ошибок, но когда я нажимал его, цвет не изменялся на черный.

В чем дело? Не могли бы вы мне помочь? Спасибо.

1 Ответ

0 голосов
/ 13 мая 2018

TemplateBinding ограничено и не работает в триггерах шаблона.Альтернативой является использование TemplatedParent относительного источника

Value="{Binding PressBackground, RelativeSource={RelativeSource TemplatedParent}}"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...