【问题标题】:Update source trigger when a button is clicked?单击按钮时更新源触发器?
【发布时间】:2011-12-05 07:52:17
【问题描述】:

我的 Wpf-App (MVVM) 中有一个内容控件,它绑定到一个对象并在文本框中显示对象属性,因此用户可以编辑属性的值。 我必须在我的控件“确定”和“取消”中设置按钮。我想使用 UpdateSourceTrigger="Explicit" 以便仅在用户明确单击“确定”时更新 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>

【问题讨论】:

    标签: mvvm


    【解决方案1】:

    您需要在代码隐藏中进行:

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

    【讨论】:

    • MVVM 并不意味着您不能编写任何代码隐藏...无论如何都无法从 ViewModel 访问 BindingExpression
    • @user1081106:您必须以某种方式对其进行编码。虚拟机不应该是这个地方。另一种选择是使用行为样式的绑定,但这样会大大增加复杂性而回报很少。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-28
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多