为什么WPF UpdateSourceTrigger没有显式绑定? [英] Why is wpf UpdateSourceTrigger not binding explicitly?

查看:115
本文介绍了为什么WPF UpdateSourceTrigger没有显式绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我的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而不是该T​​extColumn:

<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修改值时将不会触发.因此,最后,您将能够从后面的代码中保存所做的修改.

I have two-way binding between a DataGrid and an object. I want to only save changes made in the DataGrid to the object when the user clicks a save button. As a first step, I set UpdateSourceTrigger=Explicit. I would have expected no binding to occur when I set that property to explicit (since I have not called UpdateSource() yet), but contrary to my expectation, the changes are bound to the object when I close and restart the program.

Why are my changes still binding to my object's settings?

Here is my relevant DataGrid code from my xaml file:

<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>

解决方案

Try with a DataGridTemplateColumn instead of that 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>

Data class is here :

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 { };
}

And my test code:

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;
    }
}

It will get to the PropertyChanged events only at the beginning and after that, when modifying values from the GUI it won't trigger. So at the end, you will be able to save your modifications from code behind.

这篇关于为什么WPF UpdateSourceTrigger没有显式绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆