在WPF MVVM按钮上单击如何让我的视图在命令引用视图模型之前或之后做一些事情



在一个表单上我有一个标签和一个编辑按钮。单击编辑按钮后,标签的控件模板将更改为显示一个文本框和一个保存按钮。那个保存按钮与视图模型上的保存命令绑定在一起。

我的问题/问题是,当保存按钮被单击时,我希望它将控制模板更改为在视图模型上执行命令之前或之后的标签。在我的特殊情况下,它所需要做的就是在我的标签上设置一个属性为True,每当Save按钮被点击时,除了正在执行的命令。

conv:ReadOnlyControlTemplate.DoLock="True"

感谢下面回答中的一些反馈,我现在离目标更近了。我使用以下代码保存按钮:

<i:Interaction.Triggers>
                                        <ei:DataTrigger Comparison="Equal" Binding="{Binding Test, Converter={StaticResource TestConverter}, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" Value="CommandUpdated"  >
                                            <ei:ChangePropertyAction  PropertyName="conv:ReadOnlyControlTemplate.DoLock" Value="True" TargetObject="{Binding ElementName=ShiftManagerMessages}" /> 
                                        </ei:DataTrigger>
                                    </i:Interaction.Triggers>

将ei:ChangePropertyAction的TargetName更改为TargetObject导致它正确地看到Label。但是现在我得到以下错误:

{"无法找到名为的属性"conv:ReadOnlyControlTemplate。DoLock" on type "Label"。}

我可以把它指向其他属性,就是不指向这个,我不明白为什么?

使用DataTrigger控制模板的示例:

<UserControl x:Class="NextPlc.Instore.Epos.Till.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
            xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
    <ControlTemplate TargetType="Label" x:Key="ReadOnlyTemplate"></ControlTemplate>
    <ControlTemplate TargetType="Label" x:Key="EditTemplate"></ControlTemplate>
</UserControl.Resources>
<i:Interaction.Triggers>
    <ei:DataTrigger Binding="{Binding IsReadOnly}" Value="True">
        <ei:DataTrigger.Actions>
            <ei:ChangePropertyAction TargetName="label" PropertyName="Template" Value="{StaticResource ReadOnlyTemplate}"/>
        </ei:DataTrigger.Actions>
    </ei:DataTrigger>
    <ei:DataTrigger Binding="{Binding IsReadOnly}" Value="False">
        <ei:DataTrigger.Actions>
            <ei:ChangePropertyAction TargetName="label" PropertyName="Template" Value="{StaticResource EditTemplate}"/>
        </ei:DataTrigger.Actions>
    </ei:DataTrigger>
</i:Interaction.Triggers>
<Grid>
    <Label x:Name="label">
    </Label>
</Grid>

代码隐藏是一个选项,但是要考虑如果命令失败该怎么办。如果你有一个ViewModel,你可以使用绑定到属性的数据触发器或模板选择器。

我上周发布了一个关于如何编写数据模板选择器并将控件绑定到它的答案…基于ViewModel属性改变View和ViewModel

用一个datattrigger来做这个——添加一个对Microsoft.Expressions的引用。交互,然后添加XAML…

xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 

,然后…

<i:Interaction.Triggers>
    <ei:DataTrigger Binding="{Binding ViewModel.State}" Value="CommandUpdated">
         <Setter Property="Foreground" Value="Red" />
    </ei:DataTrigger>
</i:Interaction.Triggers>

这是通过基于视图模型中的属性'State'来触发的。在这种情况下,state是一个枚举,其中commandupupdated是该枚举的一个值——它可以很容易地是bool或int。

不确定我是否得到了正确的问题,但你可以运行一些代码在你的视图通过订阅各种(预览)鼠标下降事件你的按钮和改变你的布局那里。也就是说,如果您不反对在MVVM项目中使用代码。然后,您的按钮将触发VM中的命令处理程序和视图中的事件处理程序。

<Button MouseDown="Button_MouseDown" Command="{Binding SaveCommand}" />
 private void Button_MouseDown(object sender, System.Windows.RoutedEventArgs e)
 {
    // Set your property
 }

最新更新