积极答复者
Property="IsPressed" Value="True"> 时设置背景图片怎么无效?

问题
答案
-
OK, 还有一个你要注意,Button默认的行为你也要进行覆盖,因为Button使用VisualState来动画更改每个状态的,比如IsPressed 和 IsMosueOver ,而这些是写在 ButtonChrome 的样式里面的,我们无法改掉。所以方法就是全部重写Button的样式:
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="B"> <Button.Style> <Style TargetType="Button"> <Setter Property="Height" Value="37"></Setter> <Setter Property="Width" Value="34"></Setter> <Setter Property="Background"> <Setter.Value> <SolidColorBrush>Red</SolidColorBrush> </Setter.Value> </Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ButtonBase"> <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"> <ContentPresenter Content="{TemplateBinding ContentControl.Content}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" Margin="{TemplateBinding Control.Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" /> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" > <Setter.Value> <SolidColorBrush>Blue</SolidColorBrush> </Setter.Value> </Setter> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter Property="Background"> <Setter.Value> <SolidColorBrush>Green</SolidColorBrush> </Setter.Value> </Setter> </Trigger> </Style.Triggers> </Style> </Button.Style> </Button>
你自己把我里面的 SolidColorBrush换成你的ImageBrush即可。
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 a013strife 2011年10月13日 0:44
全部回复
-
因为你的控件本身已经被设置了一个Background属性值,比如<Button Background="xxx" />
这样直接在控件对象中指定属性的值,我们叫做Local Value 他的优先级最高,是Style中任何值都无法覆盖的。所以你的Trigger也无法覆盖他。解决方法是,你要把你的Background值设置放入Style比如:
<Button> <Button.Style> <Style TargetType="{x:Type Button}"> <Setter Property="Background" Value="xxx"/> <Style.Triggers> <Trigger Property="IsPressed" Value="True"> <Setter Property="Background" > <Setter.Value> <ImageBrush ImageSource="Resources/Main/上一个_点击.png"> </ImageBrush> </Setter.Value> </Setter> </Trigger> </Style.Triggers> </Style> </Button.Style> </Button>
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
<Style TargetType="Button" x:Key="NavUpButton">
<Setter Property="Height" Value="37"></Setter>
<Setter Property="Width" Value="34"></Setter>
<Setter Property="Background" >
<Setter.Value>
<ImageBrush ImageSource="Resources/Main/上一个.png">
</ImageBrush>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" >
<Setter.Value>
<ImageBrush ImageSource="Resources/Main/上一个_悬停.png">
</ImageBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
<GradientStop Color="{Binding Background.Color,RelativeSource={RelativeSource TemplatedParent}}" Offset="0.0"></GradientStop>
</LinearGradientBrush>
<!--<ImageBrush ImageSource="Resources/Main/上一个_点击.png">
</ImageBrush>-->
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers> -
OK, 还有一个你要注意,Button默认的行为你也要进行覆盖,因为Button使用VisualState来动画更改每个状态的,比如IsPressed 和 IsMosueOver ,而这些是写在 ButtonChrome 的样式里面的,我们无法改掉。所以方法就是全部重写Button的样式:
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="B"> <Button.Style> <Style TargetType="Button"> <Setter Property="Height" Value="37"></Setter> <Setter Property="Width" Value="34"></Setter> <Setter Property="Background"> <Setter.Value> <SolidColorBrush>Red</SolidColorBrush> </Setter.Value> </Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ButtonBase"> <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"> <ContentPresenter Content="{TemplateBinding ContentControl.Content}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" Margin="{TemplateBinding Control.Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" /> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" > <Setter.Value> <SolidColorBrush>Blue</SolidColorBrush> </Setter.Value> </Setter> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter Property="Background"> <Setter.Value> <SolidColorBrush>Green</SolidColorBrush> </Setter.Value> </Setter> </Trigger> </Style.Triggers> </Style> </Button.Style> </Button>
你自己把我里面的 SolidColorBrush换成你的ImageBrush即可。
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 a013strife 2011年10月13日 0:44