繁体   English   中英

触发器不适用于自定义控件图像按钮 wpf

[英]Trigger not working on custom control image button wpf

我创建了一个自定义控件

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

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

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

 <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 触发。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM