【问题标题】:How do I assign an EventTrigger to a reusable Button in its parent?如何将 EventTrigger 分配给其父级中的可重用按钮?
【发布时间】:2015-08-12 10:17:02
【问题描述】:

我的可复用Button基本上是一个按钮,其中ControlTemplate由一个TextBlock和一个Image组成. TextBlockText 属性绑定到要公开的 DependencyProperty;同样,ImageSource 属性绑定到 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>

其中 ScreenTitleScreenIcon 是前面提到的 DependecyProperty

现在,我想在它的“父级”UserControl 中使用这个Button。代码会像

<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>

但是,为了在单击 Button 时添加反应(比如更改 TextBlock "txtBlk" 的 Grid.ColumnSpan 属性到 2),我想做的其他事情是将 EventTriggers 分配给“父级”中我可重用的 Button。我一开始想了两种方法,但都没有。

  • 在我的可重用 Button 中,将 Style.Triggers 绑定到 DependencyProperty 以暴露给它的“父级”。但是,它会弹出“Triggers 属性没有可访问的设置器”。
  • 将我的可重用ButtonStyle移动到一个ResourceDictionary并为“父”分配一个Key使用。但是,通过这样做,我不确定如何处理我的两个 DependencyProperty,因为它不应该具有 ResourceDictionary 文件的代码隐藏。

还有其他解决方法吗?提前致谢。

【问题讨论】:

    标签: c# wpf styles eventtrigger


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 2021-03-11
      • 2014-03-02
      • 2014-01-21
      • 2021-05-09
      • 2014-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多