Wpf 按钮数据触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30330179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:08:45  来源:igfitidea点击:

Wpf button data trigger

wpfxamlmvvmattachment

提问by user3089816

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.

我的 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>    

回答by Rachel

If Button.Contentis set in the <Button>tag itself like you have, it will take precedence over any styled values.

如果像您一样Button.Content<Button>标签本身中设置,它将优先于任何样式值。

You need to move your StackPanel out of the <Button.Content>tag directly, and put it in the style or another data trigger instead.

您需要将您的 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>

See MSDN's Dependency Property Precedence Listfor more information.

有关更多信息,请参阅MSDN 的依赖属性优先列表