Как назначить EventTrigger повторно используемой кнопке в ее родительском элементе?

Моя многоразовая Button — это, по сути, одна кнопка, в которой ControlTemplate состоит из TextBlock и Image. Свойство Text элемента TextBlock привязывается к DependencyProperty для предоставления; аналогичным образом свойство Source объекта Image привязывается к DependencyProperty. Вот код этой кнопки.

<Button x:Class="Core.Resource.UserControlResource.NavigationButton1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Core.Resource.UserControlResource"
         mc:Ignorable="d" 
         x:Name="myself">
<Button.Style>
    <Style TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="63"></ColumnDefinition>
                            <ColumnDefinition Width="63"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Image Name="IconImage" Height="42" Width="42" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Source="{Binding ElementName=myself, Path=ScreenIcon}" />
                        <TextBlock Grid.Column="1" Text="{Binding ElementName=myself, Path=ScreenTitle}" FontSize="25" TextWrapping="Wrap" VerticalAlignment="Center"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Button.Style>

where ScreenTitle and ScreenIcon are the aforementioned DependecyProperty.

Теперь я хочу использовать эту Button в ее "родительском" элементе UserControl. Код будет таким

<UserControl x:Class="Core.ParentControl"
         x:Name="parent"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 xmlns:usrCtrlResrc="clr-namespace:Core.Resource.UserControlResource;assembly=Core.Resource"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         >
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="32"/>
        <ColumnDefinition Width="32"/>
    <Grid.ColumnDefinitions>
    <usrCtrlResrc:NavigationButton1 ScreenTitle="sample Screen Title" ScreenIcon="/Core.Resource;component/MediaResource/pencilEdit.png">
    <TextBlock Grid.Column="1" Name="txtBlk" Text="SampleSample"/>
</Grid>

However, in order to add reactions when the Button is clicked (say to change the Grid.ColumnSpan property of TextBlock "txtBlk" to 2), what I want to do else is assign EventTriggers to my reusable Button in the "parent". I initially thought of two ways, but none of them works.

  • В моей многоразовой Button привяжите Style.Triggers к DependencyProperty, чтобы получить доступ к его «родителю». Однако всплывает сообщение «Свойство Triggers не имеет доступного установщика».
  • Переместите Style моей повторно используемой Button в ResourceDictionary и назначьте Key для использования «родителем». Однако, делая это, я не знаю, как обращаться с двумя моими DependencyProperty, поскольку предполагается, что у них не должно быть кода программной части для файла ResourceDictionary.

Любые другие обходные пути? Заранее спасибо.


person Juniver Hazoic    schedule 12.08.2015    source источник


Ответы (1)


Я, наконец, решаю эту проблему, напрямую переопределяя ее триггеры. Код приведен ниже.

<usrCtrlResrc:NavigationButton1 ScreenTitle=... ScreenIcon=...>
    <usrCtrlResrc:NavigationButton1.Triggers>
        <EventTrigger RoutedEvent="usrCtrlResrc:NavigationButton1.Click">
            <EventTrigger.Actions>
                <BeginStoryboard>
                    <Storyboard>
                        ...
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger.Actions>
        </EventTrigger>
    </usrCtrlResrc:NavigationButton1.Triggers>
</usrCtrlResrc:NavigationButton1>
person Juniver Hazoic    schedule 13.08.2015