【问题标题】:Trigger not working on custom control image button wpf触发器不适用于自定义控件图像按钮 wpf
【发布时间】:2020-06-03 09:08:08
【问题描述】:

我已经创建了一个自定义控件

Generic.xaml 代码如下 -

<BitmapImage x:Key="RightImaGE" x:Name="imgD" UriSource="/XYZ.UI_Test;components/Resources/Pfad55-1.png"/>

    <Style TargetType="{x:Type local1:CustomButton}" >
        <Setter Property="Template">
             <Setter.Value>
                <ControlTemplate TargetType="{x:Type local1:CustomButton}">
                    <!--<Image Name="imgDefault" Source="{TemplateBinding Image}"  Stretch="UniformToFill" />-->
                    <ControlTemplate.Triggers>

                        <DataTrigger Binding="{Binding Path=ContentOff, RelativeSource={RelativeSource  AncestorType=local1:CustomButton}}" Value="SETTINGS">
                            <Setter Property="Background" Value="Red"/>
                            <Setter  Property="Image.Source" Value="{DynamicResource RightImaGE}" />
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

CustomButton.cs 代码隐藏如下

public string ContentOff
        {
            get { return (string)GetValue(ContentOffProperty); }
            set { SetValue(ContentOffProperty, value); }
        }
        public static readonly DependencyProperty ContentOffProperty =
            DependencyProperty.Register("ContentOff",
            typeof(string), typeof(CustomButton), new PropertyMetadata(null));

        public ImageSource Image
        {
            get { return (ImageSource)GetValue(ImageProperty); }
            set { SetValue(ImageProperty, value); }
        }

        public static readonly DependencyProperty ImageProperty =
            DependencyProperty.Register("Image", typeof(ImageSource), typeof(CustomButton), new PropertyMetadata(default(ImageSource)));

我在 app.xaml 代码中定义了一种常用样式,如下

<Application.Resources>
        <Style x:Key="BigButtonStyle" TargetType="local:CustomButton" >
            <Setter Property="Control.Background" Value="Transparent"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="Margin" Value="1"/>
            <Setter Property="FontSize" Value="27"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="Foreground" Value="#1d5560"/>
            <Setter Property="Width" Value="170"/>
            <Setter Property="MaxHeight" Value="50"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:CustomButton}">
                        <Canvas Margin="1">
                            <Image x:Name="img" Source="Resources/Pfad55.png"  Stretch="UniformToFill" >
                                <Image.Style>
                                    <Style TargetType="{x:Type Image}">
                                        <Setter Property="Source" Value="Resources/Pfad55.png" />
                                        <!--<Style.Triggers>
                                            --><!--<Trigger Property="ContentOff" Value="SETTINGS">
                                                <Setter Property="Opacity" Value="0.5" />
                                            </Trigger>--><!--
                                        </Style.Triggers>-->
                                    </Style>
                                </Image.Style>
                            </Image>
                            <ContentPresenter HorizontalAlignment="Center" Content="{TemplateBinding ContentOff}" Canvas.Top="14" Canvas.Left="47"
                                      VerticalAlignment="Center"/>
                        </Canvas>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

现在我有 Mainwindows.xaml ,我在其中使用 Custombutton ,如果按钮内容是 Settings ,它应该通过触发器更改不同的图像,而不是 bigbuttonstyle 中定义的普通图像。

<Controls:CustomButton  Grid.Row="8" Grid.Column="2" Style="{StaticResource BigButtonStyle}"
                               ContentOff="SETTINGS" 
                               Image="Resources/Pfad55-1.png"/>

我在这里尝试使用数据触发器但不工作并尝试了属性触发器。但是图像没有以某种方式改变。

【问题讨论】:

    标签: c# wpf xaml wpf-controls datatrigger


    【解决方案1】:

    您可以使用两种不同的样式与样式继承 -

     <Style x:Key="SmallButtonStyle" TargetType="local:CustomButton" BasedOn="{StaticResource BigButtonStyle}">
               <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type local:CustomButton}">
                            <Canvas Margin="1">
                                <Image Source="Resources/Pfad55-1.png"  Stretch="UniformToFill"  ></Image>
                                <ContentPresenter HorizontalAlignment="Center" Content="{TemplateBinding ContentOff}" Canvas.Top="14" Canvas.Left="85"
                                          VerticalAlignment="Center"/>
                            </Canvas>
    
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    

    然后使用它

        <Controls:CustomButton  Grid.Row="8" Grid.Column="2" Style="{StaticResource SmallButtonStyle}"
                           ContentOff="SETTINGS" 
                           Image="Resources/Pfad55-1.png"/>
    

    它会工作,但没有触发器。

    我根据您的要求添加了另一个解决方案。 更改您的 Generic.Xaml 代码,如下所示。

    <Style TargetType="{x:Type local1:CustomButton}" >
        <Setter Property="FontSize" Value="27"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="Foreground" Value="#1d5560"/>
        <Setter Property="Template">
             <Setter.Value>                
                <ControlTemplate TargetType="{x:Type local1:CustomButton}">
                    <Grid>
                        <Image  Stretch="Fill" >
                            <Image.Style>
                                <Style TargetType="{x:Type Image}">
                                    <Setter Property="Source" Value="Resources/Pfad55.png" />
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local1:CustomButton},Path=ContentOff}"
                                            Value="SETTINGS">
                                            <Setter Property="Source"
                                                    Value="Resources/Pfad55-1.png" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Image.Style>
                        </Image>
                        <ContentPresenter HorizontalAlignment="Left" Margin="50,0,0,0" Content="{TemplateBinding ContentOff}" 
                                      VerticalAlignment="Center"/>
                    </Grid>
                </ControlTemplate>
    
            </Setter.Value>
        </Setter>
    </Style>
    

    然后用这个改变你的 MainWindow.xaml 实现

    <Controls:CustomButton  Grid.Row="8" Grid.Column="2" ContentOff="SETTINGS"/>
    

    您将 App.xaml 样式与 BigButtonStyle 一起使用,这就是自定义控件样式和触发器未触发的原因。您的 App.xaml 样式是本地覆盖自定义控件样式并从 Generic.xaml 触发。

    【讨论】:

    • 但不知何故要求是触发。
    猜你喜欢
    • 2017-06-15
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 2011-01-23
    相关资源
    最近更新 更多