Wpf按钮数据触发器

时间:2015-05-19 15:39:59

标签: wpf xaml mvvm attachment

我有一个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 个答案:

答案 0 :(得分: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's Dependency Property Precedence List