WPF按钮数据触发器 [英] Wpf button data trigger
问题描述
我在wpf表单中有一个按钮,当我单击按钮时它将在mvvm应用程序中显示图像文本,它将附加文件,我的要求是在成功附加时我要从按钮中删除图像,想要用一些文本更新按钮.
i have a button in my wpf form and the button is having the image text in mvvm application when i click the button it will attach the file, my requirement is when it attached successfully i want to remove the image from the button and want to update the button with some text.
<StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Right">
<Button ToolTip="Attach Approval"
Height="25"
Command="{Binding AddAttachmentCommand}"
Margin="5,10,5,10">
<StackPanel Orientation="Horizontal">
<Image Source="/UILibrary;component/Themes/Default/Images/Attach.PNG"/>
</StackPanel>
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsAttachmentAvailable}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
<Setter Property="Content" Value="Appprove"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsAttachmentAvailable}" Value="False">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<StackPanel Orientation="Horizontal"
Height="25"
Margin="5,10,5,10"
Visibility="{Binding IsAttachmentAvailable, Converter={StaticResource BooleanToVisibilityConverter}}">
<TextBlock Margin="3">
<Hyperlink Command="{Binding OpenAttachmentCommand}">
<TextBlock Text="{Binding Attachment.FileName}"/>
</Hyperlink>
</TextBlock>
<customControls:CloseButton Width="15" Height="15" Command="{Binding RemoveAttachmentCommand}">
<customControls:CloseButton>
Remove attachment
</customControls:CloseButton>
</customControls:CloseButton>
</StackPanel>
<Button Height="25"
Width="80"
Margin="5,10,5,10"
Content="Approve"
Command="{Binding ApproveTemplateCommand}"/>
<Button Height="25"
Width="80"
Margin="5,10,5,10"
Content="Preview"
Command="{Binding PreviewTemplateCommand}"/>
<Button Content="Save"
Command="{Binding SaveTemplateCommand}"
Height="25"
Width="80"
Margin="5,10,5,10"/>
<Button Height="25"
Width="80"
Margin="5,10,10,10"
Content="Cancel"
Command="{Binding CancelCommand}"/>
</StackPanel>
推荐答案
如果像您一样在<Button>
标记本身中设置了Button.Content
,它将优先于任何样式化的值.
If Button.Content
is set in the <Button>
tag itself like you have, it will take precedence over any styled values.
您需要将StackPanel直接移出<Button.Content>
标记,然后将其置于样式或其他数据触发器中.
You need to move your StackPanel out of the <Button.Content>
tag directly, and put it in the style or another data trigger instead.
<Button ToolTip="Attach Approval"
Height="25"
Command="{Binding AddAttachmentCommand}"
Margin="5,10,5,10">
<Button.Style>
<Style TargetType="{x:Type Button}">
<!-- Default Content value -->
<Setter Property="Content">
<Setter.Value>
<StackPanel Orientation="Horizontal">
<Image Source="/UILibrary;component/Themes/Default/Images/Attach.PNG"/>
</StackPanel>
</Setter.Value>
</Setter>
<!-- Triggered values -->
<Style.Triggers>
<DataTrigger Binding="{Binding IsAttachmentAvailable}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
<Setter Property="Content" Value="Appprove"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsAttachmentAvailable}" Value="False">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
请参见 MSDN的依赖属性优先级列表更多信息.
这篇关于WPF按钮数据触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!