Mvvm 单击按钮时是否更新源触发器?

Mvvm 单击按钮时是否更新源触发器?,mvvm,Mvvm,我的Wpf应用程序(MVVM)中有一个contentcontrol,它绑定到一个对象,并在文本框中显示对象属性,因此用户可以编辑属性值。 我必须在我的控制按钮“确定”和“取消”。。我希望使用UpdateSourceTrigger=“Explicit”,这样只有当用户显式单击“确定”时,ViewModel才会更新,而不是在修改文本框值时更新。我怎样才能做到这一点 这就是我的viewmodel的外观 class NeedViewModel : CarePlanEntityViewModelBase

我的Wpf应用程序(MVVM)中有一个contentcontrol,它绑定到一个对象,并在文本框中显示对象属性,因此用户可以编辑属性值。 我必须在我的控制按钮“确定”和“取消”。。我希望使用UpdateSourceTrigger=“Explicit”,这样只有当用户显式单击“确定”时,ViewModel才会更新,而不是在修改文本框值时更新。我怎样才能做到这一点

这就是我的viewmodel的外观

class NeedViewModel : CarePlanEntityViewModelBase
{
    public CPLNursingNeedVersion CurrentNeedVersion
    {
        get { return currentNeedVersion; }
        set 
        { 
            currentNeedVersion = value;
            this.OnPropertyChanged("CurrentNeedVersion");
        }
    } 
}
这就是我的观点

<DataTemplate x:Key="NeedDataEntryDataTemplate">
    <Border Background="White" 
            BorderThickness="2" 
            BorderBrush="#4682B4" 
            local:PublishReadOnlyProperty.Observe="True" 
            local:PublishReadOnlyProperty.ActualWidth="{Binding Path=Width,Mode=OneWayToSource,UpdateSourceTrigger=PropertyChanged}"
            MinWidth="410">
        <Grid Name="mainGrid">
            <Grid.BindingGroup>
                <BindingGroup>
                </BindingGroup>
            </Grid.BindingGroup>
            <Grid.RowDefinitions>
                <RowDefinition  Height="28" MinHeight="28"/>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>

            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="7" MaxWidth="7"></ColumnDefinition>

                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="68" MaxWidth="75"></ColumnDefinition>
                <ColumnDefinition Width="10" MaxWidth="10"></ColumnDefinition>
                <ColumnDefinition Width="68" MaxWidth="75"></ColumnDefinition>

            </Grid.ColumnDefinitions>


            <Label Padding="0,3,0,3" 
                   Name="label9" 
                   Grid.Row="1" Grid.Column="1" 
                   Margin="3" 
                   FontSize="13" 
                   HorizontalAlignment="Left" >Voimassaoloaika:</Label>
            <hcw:HCWDateTimePicker Padding="0,3,0,3" 
                                   x:Name="startDateTimePicker" 
                                   Grid.Row="1" Grid.Column="2" 
                                   Margin="5,3,3,3"  
                                   FontSize="13"  
                                   HorizontalAlignment="Left" 
                                   MaxWidth="120" 
                                   DateControlMode="Mandatory"    
                                   SelectedDate="{Binding Path=CurrentNeedVersion.ValidFromDate,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                                   DisplayMode="DateOnly" />
            <Label Padding="0,3,0,3" 
                   Name="label8"  
                   Grid.Row="1" Grid.Column="3" 
                   Margin="3,3,3,3" 
                   HorizontalAlignment="Center"  
                   FontSize="13">-</Label>
            <hcw:HCWDateTimePicker x:Name="endDateTimePicker"
                                   Padding="0,3,0,3" 
                                   Grid.ColumnSpan="5" Grid.Row="1" Grid.Column="4"
                                   Margin="3" 
                                   FontSize="13" 
                                   HorizontalAlignment="Left" 
                                   MaxWidth="120" 
                                   DisplayMode="DateOnly"
                                   SelectedDate="{Binding Path=CurrentNeedVersion.ValidToDate,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

            <Label Name="label26" 
                   Padding="0,3,0,3" 
                   FontSize="13" 
                   Grid.Row="2" Grid.Column="2" 
                   Margin ="20,3,3,3" 
                   HorizontalAlignment="Left" 
                   Grid.ColumnSpan="6">Huomiomerkitty</Label>
            <CheckBox Padding="0,3,0,10" 
                      Grid.Row="2" Grid.Column="2"
                      IsChecked="{Binding  Path=CurrentNeedVersion.IsNoteworthy,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                      Name="note" 
                      FontSize="13" 
                      Margin="3,3,20,3" 
                      VerticalAlignment="Center"  
                      HorizontalAlignment="Left" 
                      IsEnabled="True" />


            <ContentControl Content="{Binding}" 
                            Grid.Row="3" Grid.ColumnSpan="9" 
                            ContentTemplate="{DynamicResource ClassificationSelectorDataTemplate}"/>

            <Label Padding="0,3,0,3" 
                   Grid.Row="4" Grid.Column="1" 
                   Margin="3" 
                   FontSize="13"  
                   Name="needLabel" 
                   HorizontalAlignment="Left" >Tarve:</Label>
            <hcw:HCWTextBox Padding="3,3,0,3" 
                            Grid.Row="4" Grid.Column="2" Grid.ColumnSpan="6"
                            FontSize="13" 
                            Margin="3" 
                            Name="needTextBox" 
                            Text="{Binding Path=CurrentNeedVersion.NeedText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  
                            TextWrapping="Wrap" 
                            AcceptsReturn="True" />

            <Label Padding="0,3,0,3" 
                   Margin="3" 
                   Name="goalLabel" 
                   FontSize="13" 
                   Grid.Row="8" Grid.Column="1" Grid.ColumnSpan="1">Tavoite:</Label>
            <TextBox Padding="0,3,0,3" 
                     Grid.Row="8" GridColumn="2" Grid.ColumnSpan="6" 
                     FontSize="13" 
                     Margin="3"  
                     Text="{Binding Path=CurrentNeedVersion.GoalText, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                     HorizontalAlignment="Stretch" 
                     Name="goalTextBox" 
                     TextWrapping="Wrap" 
                     AcceptsReturn="True" />

            <Label Name="temporaryLabel" 
                   Padding="0,3,0,3" 
                   FontSize="13" 
                   Grid.Row="9" Grid.Column="2" Grid.ColumnSpan="6"
                   Margin="20,3,3,3" 
                   HorizontalAlignment="Left">Keskeneräinen</Label>
            <CheckBox Padding="0,3,0,10" 
                      Grid.Row="9" Grid.Column="2" 
                      Name="temporaryCheckBox" 
                      FontSize="13" 
                      Margin="3,3,20,3" 
                      VerticalAlignment="Center" 
                      HorizontalAlignment="Left" 
                      IsEnabled="True" />

            <Button Name="acceptButton" 
                    Command="{Binding Path=AcceptNeedDEC}" 
                    Margin="0,0,0,7" 
                    Grid.Row="10" Grid.Column="5" 
                    FontSize="13" 
                    MinWidth="68" 
                    Content="OK" 
                    VerticalAlignment="Center" 
                    Background="WhiteSmoke" />

            <Button Command="{Binding Path=CloseDEC}" 
                    Name="cancelButton" 
                    Margin="0,0,0,7" 
                    Grid.Row="10" Grid.Column="7"  
                    HorizontalAlignment="Right" 
                    FontSize="13" 
                    MinWidth="68" 
                    Content="Peruuta"   
                    VerticalAlignment="Center" 
                    Background="WhiteSmoke"  />

        </Grid>
    </Border>
</DataTemplate>

Voimassaoloaika:
-
霍米尔基蒂
塔维:
塔瓦特:
凯斯肯纳伊宁

您需要在代码隐藏中执行此操作:

var bindingExpression = BindingOperations.GetBindingExpression(startDateTimePicker, HCWDateTimePicker.SelectedDateProperty);
bindingExpression.UpdateSource();

您可以获取视图绑定到的所有属性,并将它们放置在不同的类中。然后在ViewModel中有该类的两个实例,一个用于数据绑定,一个用于私有。单击按钮后,将值从公共值复制到私有值,然后针对私有值运行所有逻辑。

MVVM并不意味着您不能在后面编写任何代码。。。无法从ViewModel访问BindingExpressionanyway@user1081106:无论如何,你必须在某个地方对它进行编码。虚拟机不应该是这个地方。另一种选择是使用行为样式绑定,但这会大大增加复杂性,但回报很小。