[英]In WPF MVVM on button click how to have my view do something before or after the Command reference to the view model?
在一種形式上,我有一個標簽和一個編輯按鈕。 單擊“編輯按鈕”時,標簽的控件模板將更改為顯示“文本框”和“保存按鈕”。 該“保存”按鈕與視圖模型上的“保存”命令綁定在一起。
我的問題是,當單擊“保存”按鈕時,我希望它在視圖模型上執行命令之前或之后將控件模板改回為標簽。 在我特定的情況下,除了執行命令外,每當單擊“保存”按鈕時,只需將標簽上的屬性設置為True。
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 \\”的屬性。“}
我可以將其指向其他屬性,但不能指向其他屬性,我也不明白為什么?
不確定我是否解決了問題,但是您可以通過訂閱按鈕的各種(preview)mousedown事件並在其中更改布局,在視圖中運行一些代碼。 也就是說,如果您不反對在MVVM項目中使用代碼。 然后,您的按鈕將同時觸發VM中的命令處理程序和視圖中的事件處理程序。
<Button MouseDown="Button_MouseDown" Command="{Binding SaveCommand}" />
private void Button_MouseDown(object sender, System.Windows.RoutedEventArgs e)
{
// Set your property
}
后面的代碼是一個選項,但是請考慮如果命令失敗,該怎么辦。 如果那里有ViewModel,則可以使用綁定到屬性的數據觸發器或模板選擇器。
上周,我發布了一個有關如何編寫數據模板選擇器並將其綁定到控件的答案... 基於ViewModel屬性的ViewModel更改視圖
使用DataTrigger進行此操作-添加對Microsoft.Expressions.Interactions的引用,然后添加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”觸發而起作用的。 在這種情況下,狀態為Enumeration,其中CommandUpdated是該枚舉的值-它可以很容易地成為bool或int。
使用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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.