问题描述
我使用一个名为自定义控件的ImageButton
来显示与不同图像的按钮。 的ImageButton
包含依赖属性:
公开的ImageSource DisabledImageSource
{
{返回(ImageSource的)的GetValue(DisabledImageSourceProperty); }
集合{的SetValue(DisabledImageSourceProperty,值); }
}
公众的ImageSource NormalImageSource
{
{返回(ImageSource的)的GetValue(NormalImageSourceProperty); }
集合{的SetValue(NormalImageSourceProperty,值); }
}
公众的ImageSource HoverImageSource
{
{返回(ImageSource的)的GetValue(HoverImageSourceProperty); }
集合{的SetValue(HoverImageSourceProperty,值); }
}
公众的ImageSource PushedImageSource
{
{返回(ImageSource的)的GetValue(PushedImageSourceProperty); }
集合{的SetValue(PushedImageSourceProperty,值); }
}
公共静态只读的DependencyProperty DisabledImageSourceProperty =
DependencyProperty.Register(DisabledImageSource的typeof(ImageSource的)的typeof(ImageButton的),新UIPropertyMetadata() );
公共静态只读的DependencyProperty NormalImageSourceProperty =
DependencyProperty.Register(NormalImageSource的typeof(ImageSource的)的typeof(ImageButton的),新UIPropertyMetadata());
公共静态只读的DependencyProperty HoverImageSourceProperty =
DependencyProperty.Register(HoverImageSource的typeof(ImageSource的)的typeof(ImageButton的),新UIPropertyMetadata());
公共静态只读的DependencyProperty PushedImageSourceProperty =
DependencyProperty.Register(PushedImageSource的typeof(ImageSource的)的typeof(ImageButton的),新UIPropertyMetadata());
据的风格定义如下:
<风格的TargetType ={X:类型本地:ImageButton的}>
< setter属性=的HorizontalAlignmentVALUE =左/>
< setter属性=模板>
< Setter.Value>
<的ControlTemplate的TargetType ={X:类型按钮}>
<网格和GT;
<图像名称=形象来源={结合NormalImageSource,的RelativeSource = {的RelativeSource TemplatedParent}}拉伸=无/>
< /网格和GT;
< ControlTemplate.Triggers>
<触发属性=IsEnabledVALUE =FALSE>
<二传手的TargetName =形象属性=源VALUE ={结合DisabledImageSource,的RelativeSource = {的RelativeSource TemplatedParent}}/>
< /触发>
<触发属性=Button.IsPressedVALUE =真>
<二传手的TargetName =形象属性=源VALUE ={结合PushedImageSource,的RelativeSource = {的RelativeSource TemplatedParent}}/>
< /触发>
< MultiTrigger>
< MultiTrigger.Conditions>
<条件属性=IsMouseOverVALUE =真/>
< /MultiTrigger.Conditions>
<二传手的TargetName =形象属性=源VALUE ={结合HoverImageSource,的RelativeSource = {的RelativeSource TemplatedParent}}/>
< / MultiTrigger>
< /ControlTemplate.Triggers>
< /控件模板>
< /Setter.Value>
< /二传手>
< /样式和GT;
每一件事工作正常,除了 IsPressed
。我从来没有看到我在 PushedImageSource
设定图像和我想不通为什么。
任何帮助将appriciated:)
编辑1:
这里是XAML代码测试它
<窗口x:类=SMEClient.WPF.Tester.MainWindow
的xmlns =HTTP: //schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:是= CLR命名空间:Project1.SearchBox;装配= PROJECT1
标题=主窗口HEIGHT =350WIDTH =525>
< Window.Resources>
<&ResourceDictionary中GT;
< ResourceDictionary.MergedDictionaries>
< ResourceDictionary中源=包://应用:,,, / PROJECT1;组件/搜索盒/ ImageButton.xaml/>
< /ResourceDictionary.MergedDictionaries>
< / ResourceDictionary的>
< /Window.Resources>
<网格和GT;
<&StackPanel的GT;
将是:ImageButton的NormalImageSource =C:\Users\Public\Pictures\Sample Pictures\Koala.jpg
PushedImageSource =C:\Users\Public\ Pictures\Sample Pictures\Chrysanthemum.jpg
HoverImageSource =C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg/>
< / StackPanel的>
< /网格和GT;
< /窗GT;
您的触发器是不是相互排斥的,在这种情况下,顺序的触发器被添加到触发器
集合进场。你最后的触发 IsMouseOver
覆盖后 IsPressed
的来源
属性触发器长方砌石正确的图像,因为按下按钮只有当鼠标悬停(当然使用鼠标时)。
尝试设置了 IsPressed
触发最后在触发器
集合,而 IsPressed
触发器将被即使<$应用C $ C> IsMouseOver 也是如此。
I use a custom control named ImageButton
to display a button with different images. ImageButton
contains dependency properties:
public ImageSource DisabledImageSource
{
get { return (ImageSource)GetValue(DisabledImageSourceProperty); }
set { SetValue(DisabledImageSourceProperty, value); }
}
public ImageSource NormalImageSource
{
get { return (ImageSource)GetValue(NormalImageSourceProperty); }
set { SetValue(NormalImageSourceProperty, value); }
}
public ImageSource HoverImageSource
{
get { return (ImageSource)GetValue(HoverImageSourceProperty); }
set { SetValue(HoverImageSourceProperty, value); }
}
public ImageSource PushedImageSource
{
get { return (ImageSource)GetValue(PushedImageSourceProperty); }
set { SetValue(PushedImageSourceProperty, value); }
}
public static readonly DependencyProperty DisabledImageSourceProperty =
DependencyProperty.Register("DisabledImageSource", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata());
public static readonly DependencyProperty NormalImageSourceProperty =
DependencyProperty.Register("NormalImageSource", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata());
public static readonly DependencyProperty HoverImageSourceProperty =
DependencyProperty.Register("HoverImageSource", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata());
public static readonly DependencyProperty PushedImageSourceProperty =
DependencyProperty.Register("PushedImageSource", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata());
It's style is defined as follows:
<Style TargetType="{x:Type local:ImageButton}">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Image Name="image" Source="{Binding NormalImageSource, RelativeSource={RelativeSource TemplatedParent}}" Stretch="None" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="image" Property="Source" Value="{Binding DisabledImageSource, RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>
<Trigger Property="Button.IsPressed" Value="True">
<Setter TargetName="image" Property="Source" Value="{Binding PushedImageSource, RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="image" Property="Source" Value="{Binding HoverImageSource, RelativeSource={RelativeSource TemplatedParent}}"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Every thing works fine except IsPressed
. I can never see the image I set in PushedImageSource
and I can't figure out why. Any help would be appriciated :)
EDIT 1:
Here's the xaml code that tests it
<Window x:Class="SMEClient.WPF.Tester.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:is="clr-namespace:Project1.SearchBox;assembly=Project1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Project1;component/SearchBox/ImageButton.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<StackPanel>
<is:ImageButton NormalImageSource="C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"
PushedImageSource="C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg"
HoverImageSource="C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg"/>
</StackPanel>
</Grid>
</Window>
Your triggers are not mutual exclusive, in this case the order in which triggers are added to the Triggers
collection comes into play. Your last trigger IsMouseOver
overrides the Source
property after IsPressed
trigger setts the correct image, since a button is Pressed only if the mouse is over (when using mouse of course).
Try setting the IsPressed
trigger last in the Triggers
collection, and the IsPressed
trigger will be applied even though IsMouseOver
is also true.
这篇关于WPF:控件模板的IsPressed触发不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!