【问题标题】:How to restore Text after changes have been cancelled?取消更改后如何恢复文本?
【发布时间】:2014-08-06 22:22:26
【问题描述】:

我有一个绑定到我的 VM 上的字段的标签。标签有两个控制模板,一个用于只读模式,一个用于编辑模式。一切正常,但我有一个问题。如果您将控件置于编辑模式并开始在 TextBox 中键入内容,然后单击“取消”,它不会将其恢复为原始文本。如何强制它重新绑定文本并将文本恢复到 VM 上的内容?

<Label Grid.Column="0" Grid.Row="0" Name="ShiftManagerMessages" Content="{Binding Path=Messages, IsAsync=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }" Foreground="White" FontSize="18px" Margin="5,5,5,5" 
                                   conv:ReadOnlyControlTemplate.Enabled="False" conv:ReadOnlyControlTemplate.DoLock="{Binding Path=MessageUpdateSuccessful, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }" HorizontalAlignment="Left" HorizontalContentAlignment="Left" VerticalAlignment="Top" Height="54" Width="1150">
                                <Control.Template>
                                    <ControlTemplate TargetType="{x:Type Label}">
                                        <WrapPanel>
                                            <TextBox Width="1000" HorizontalAlignment="{TemplateBinding Property=HorizontalAlignment}" VerticalAlignment="Top" Height="150" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content, Mode=TwoWay}" TextWrapping="Wrap" AllowDrop="True" />
                                    <Button Content="Cancel" Width="65"  Height="30" HorizontalAlignment="Right" Cursor="Hand" Margin="5,0,5,0" Click="CancelShiftMessage_Click"  />
                                    <Button Content="Save" Width="50"  Height="30" HorizontalAlignment="Right" Cursor="Hand" Margin="0,0,0,0"  Command="{Binding SaveMessagesCommand}" />
                                        </WrapPanel>
                                    </ControlTemplate>
                                </Control.Template>
                                <conv:ReadOnlyControlTemplate.LockTemplate>
                                    <ControlTemplate TargetType="{x:Type Label}">
                                        <TextBlock Width="{TemplateBinding Property=Width}" Height="{TemplateBinding Property=Height}" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content, Mode=TwoWay }" Margin="0,0,0,0" Padding="0,0,0,0"
                                                   Foreground="{TemplateBinding Property=Foreground}" FontSize="{TemplateBinding Property=FontSize}" TextWrapping="Wrap"  HorizontalAlignment="{TemplateBinding Property=HorizontalAlignment}" VerticalAlignment="{TemplateBinding Property=VerticalAlignment}" /> 
                                    </ControlTemplate>
                                </conv:ReadOnlyControlTemplate.LockTemplate>
                            </Label>

我已经尝试了我所知道的唯一绑定更新的几个变体,但都没有成功。

private void CancelShiftMessage_Click(object sender, RoutedEventArgs e)
    {
        ReadOnlyControlTemplate.SetDoLock(ShiftManagerMessages, true);

        BindingOperations.GetBindingExpressionBase(ShiftManagerMessages, Label.ContentProperty).UpdateTarget();
        BindingOperations.GetBindingExpression(ShiftManagerMessages, Label.ContentProperty).UpdateTarget();

    } 

【问题讨论】:

  • 你需要在你的VM中实现IEditableObject接口并调用CancelEdit

标签: c# wpf xaml binding


【解决方案1】:

答案正如 Sriram Sakthivel 的评论所建议的那样,在我的 ViewModel 上实现 IEditableObject 接口。该接口公开了 3 个方法:BeginEdit()、CancelEdit() 和 EndEdit()。我添加了 3 个命令来配合这些方法和一个位来指示模式是否为只读。

在 BeginEdit() 中,我将编辑模式设置为 true 并复制原始消息。但是,如果我要处理多个字段,我建议使用 MemberwiseClone 获取对象的副本。但是,如果您的编辑包含任何引用类型,您将需要进行深度复制。

public void BeginEdit()
    {
        IsEditing = true;
        originalmessage = this.Messages;
    }

在 CancelEdit() 中,我将消息设置回原来的状态并更改编辑模式。

public void CancelEdit()
    {
        this.Messages = originalmessage;
        IsEditing = false;
    }

在 EndEdit() 中我保存实际更改并重置编辑模式。

public void EndEdit()
    {
        IsEditing = false;
        repository.SaveMessages(this.Messages); 
    }

在 Xaml 中,显示的控件模板绑定到 IsEditing 属性,而编辑、取消和保存命令绑定到为支持 IEditableObject 而创建的那些。

【讨论】:

    猜你喜欢
    • 2014-11-22
    • 1970-01-01
    • 2012-11-11
    • 2014-12-08
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    相关资源
    最近更新 更多