c# - 为什么 wpf UpdateSourceTrigger 没有明确绑定(bind)?

标签 c# wpf xaml datagrid updatesourcetrigger

我在 DataGrid 和对象之间有双向绑定(bind)。我只想在用户单击保存按钮时将在 DataGrid 中所做的更改保存到对象。作为第一步,我设置了 UpdateSourceTrigger=Explicit。当我将该属性设置为显式(因为我还没有调用 UpdateSource() )时,我原以为不会发生绑定(bind),但与我的预期相反,当我关闭并重新启动程序时,更改被绑定(bind)到对象。

为什么我的更改仍然绑定(bind)到对象的设置?

这是我的 xaml 文件中的相关 DataGrid 代码:

<DataGrid x:Name="DataGrid1" IsReadOnly="False"
                 AutoGenerateColumns="False" CanUserAddRows="False" SelectionUnit="Cell"
                  ItemsSource="{Binding data}">
            <DataGrid.DataContext>
                <Binding Source="{StaticResource myData}" UpdateSourceTrigger="Explicit"/>
            </DataGrid.DataContext>

            <DataGrid.Columns>
                <DataGridTextColumn Header="Field" Binding="{Binding Path=name, Mode=TwoWay, 
                        UpdateSourceTrigger=Explicit}" Width="Auto"/>
                <DataGridTextColumn Header="Length of Field" Binding="{Binding Path=length, Mode=TwoWay, 
                        UpdateSourceTrigger=Explicit}" Width="Auto"/>
            </DataGrid.Columns>
</DataGrid>

最佳答案

尝试使用 DataGridTemplateColumn 而不是 TextColumn:

<Window x:Class="DataGridUpdateSourceTriggerOneWay.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DataGridUpdateSourceTriggerOneWay"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <DataGrid x:Name="DataGrid1" 
              IsReadOnly="False"
              AutoGenerateColumns="False"
              CanUserAddRows="False"
              SelectionUnit="Cell"
              ItemsSource="{Binding data}">

        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Field">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Path=Name, Mode=TwoWay, 
                    UpdateSourceTrigger=Explicit}" Width="Auto"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Length of Field">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Path=Length, Mode=TwoWay, 
                    UpdateSourceTrigger=Explicit}" Width="Auto"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

数据类在这里:

public class Data : INotifyPropertyChanged
{
    private string _Name;

    public string Name
    {
        get { return _Name; }
        set
        {
            _Name = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Name"));
        }
    }

    private int _Length;

    public int Length
    {
        get { return _Length; }
        set
        {
            _Length = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Length"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}

还有我的测试代码:

public partial class MainWindow : Window
{
    public ObservableCollection<Data> data { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        data = new ObservableCollection<Data>();
        data.Add(new Data() { Name = "Data1", Length = 1 });
        data.Add(new Data() { Name = "Data2", Length = 2 });
        this.DataContext = this;
    }
}

它只会在开始和之后到达 PropertyChanged 事件,当从 GUI 修改值时它不会触发。所以最后,您将能够从代码隐藏中保存您的修改。

关于c# - 为什么 wpf UpdateSourceTrigger 没有明确绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30766983/

相关文章:

c# - 如何序列化到一个变量

c# - 关于在 WPF 中的 Canvas 中移动形状的一些事情

wpf - MVVM:如果后台线程不断更改模型怎么办?

c# - MVVM - 如何在行中按钮上的鼠标悬停上显示 DataGrid 中的弹出窗口

xaml - Xaml 中的 Xamarin 表单选取器项源绑定(bind)

WPF 切换按钮 XAML 样式

c# - 在 Entity Framework 6.1 中使用 SQLite 1.0.92 时遇到问题

c# - 针对 Azure/CDN 进行编程

c# - 带有 SerialPort 的 Windows 窗体 - 应用程序在关闭窗体后挂起

c# - Clr 命名空间映射到默认 xaml 命名空间