问题
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>
回答1:
If Button.Content
is set in the <Button>
tag itself like you have, it will take precedence over any styled values.
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>
See MSDN's Dependency Property Precedence List for more information.
来源:https://stackoverflow.com/questions/30330179/wpf-button-data-trigger