我正在尝试为页面实现保存功能。为此,我对我的控件进行双向绑定,并将UpdateSourceTrigger值设置为" Explicit"。我使用了显式选项,这样我的绑定视图模型属性在进行更改时不会立即更改,只有当我按下" Save"按钮。如果我按,"取消"按钮,我将离开我的页面而不更新绑定,以便我的原始值不会改变。
我想使用BindingGroup并希望一次性更新所有控件的绑定。我有如下所示的代码实现,但问题是,我没有在BindingGroup的BindingExpression集合中获得任何值,它始终保持为空。我明白,我应该得到所有绑定绑定组名称的绑定应该是BindingGroup.BindingExpressions集合的一部分。因此,即使我调用" BindingGroup.UpdateSources()",我也不会将新值更新到我的视图模型。
你能不能通过以下代码来表明错误(即我对BindingGroup或我的实现的理解)
XAML代码
<Window x:Class="WpfApplication1.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="300">
<StackPanel>
<StackPanel.BindingGroup>
<BindingGroup x:Name="Group"/>
</StackPanel.BindingGroup>
<TextBox Text="{Binding P1, Mode=TwoWay, BindingGroupName=Group, UpdateSourceTrigger=Explicit, ValidatesOnDataErrors=True}"/>
<TextBox Text="{Binding P2, Mode=TwoWay, BindingGroupName=Group, UpdateSourceTrigger=Explicit, ValidatesOnDataErrors=True}"/>
<TextBox Text="{Binding P3, Mode=TwoWay, BindingGroupName=Group, UpdateSourceTrigger=Explicit, ValidatesOnDataErrors=True}"/>
<Button Content="Update Values" Click="Button_Click"/>
</StackPanel>
</Window>
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window2.xaml
/// </summary>
public partial class Window2 : Window
{
public Window2()
{
this.DataContext = new VM();
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Group.UpdateSources();
}
}
public class VM : INotifyPropertyChanged
{
private string _p1, _p2, _p3;
public string P1
{
get
{
return _p1;
}
set
{
_p1 = value;
NotifyPropertyChanged("P1");
}
}
public string P2
{
get
{
return _p2;
}
set
{
_p2 = value;
NotifyPropertyChanged("P2");
}
}
public string P3
{
get
{
return _p3;
}
set
{
_p3 = value;
NotifyPropertyChanged("P3");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
谢谢,
答案 0 :(得分:0)
Binding应该没问题,我可以更新VM
的属性。
试试吧。
<TextBox Name="TextBox1" Text="{Binding P1, Mode=OneWayToSource, BindingGroupName=Group, UpdateSourceTrigger=Explicit, ValidatesOnDataErrors=True}"/>
<TextBox Name="TextBox2" Text="{Binding P2, Mode=OneWayToSource, BindingGroupName=Group, UpdateSourceTrigger=Explicit, ValidatesOnDataErrors=True}"/>
<TextBox Name="TextBox3" Text="{Binding P3, Mode=OneWayToSource, BindingGroupName=Group, UpdateSourceTrigger=Explicit, ValidatesOnDataErrors=True}"/>
public partial class Window2 : Window
{
public Window2()
{
this.DataContext = new VM();
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
BindingExpression be = TextBox1.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();
}
}
现在,单击按钮后,TextBox的值将刷新。