gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:06:02 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com