wpf button command binding after trigger the prope

2019-02-21 05:42发布

问题:

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 the text is changed to Approve , I want to write another command propery for this command after changed the text to Approve.

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

回答1:

If you want to change a property (in your case the Command) in a trigger, you have to initialise the property in the style setter. To make your code work remove the command property from the button and add the command property to the style setter.

<Button ToolTip="Attach Approval" 
        Height="25" 

        Margin="5,10,5,10">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <!-- Default Content value -->
            <Setter Property="Command" Value="{Binding AddAttachmentCommand}"/>
            <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"/>
                    <Setter Property="Command" Value="SOME OTHER COMMAND"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding IsAttachmentAvailable}" Value="False">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>


标签: c# wpf mvvm