Wpf按钮数据触发器

6

我在WPF表单上有一个按钮,在MVVM应用程序中,该按钮具有图像文本。当我单击按钮时,它会附加文件。我的要求是,当它成功附加时,我想从按钮中删除图像并更新按钮以显示一些文本。

 <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个回答

9
如果Button.Content<Button>标签中设置,它将优先于任何样式值。
您需要将StackPanel直接移出<Button.Content>标记,并将其放置在样式或另一个数据触发器中。
<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的依赖属性优先级列表以获取更多信息。

@user3089816 使用相同的语法,您可以将几乎任何属性添加到样式或数据触发器中:<Setter Proprety="Command" Value="{Binding AddAttachmentCommand}" /> - Rachel
@user3089816,您在<Button>标记中是否仍设置了Command属性?这里应用相同的依赖属性优先级规则:在<Tag>中定义的属性优先于在样式或数据触发器中应用的属性。 - Rachel
嗨@rachel,上面的代码中我声明了AddAttachmentCommand命令,如果附件添加成功,按钮内容将更改为批准,然后我又声明了一个命令<Setter Property="Command" Value="{Binding isapprovetemplatecommand}"/>,执行此命令后,仍会触发之前的命令(AddAttachmentCommand)。 - user3089816
@user3089816你需要分享你的代码以确定具体问题。也许可以开一个新的问题来讨论? - Rachel
谢谢,不过请尝试查看DependencyProperty Precedence List并理解这个答案。对于这两个问题,答案是相同的 :) - Rachel
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接