gpt4 book ai didi

wpf - WPF 中的 DataGrid 和 Observable 集合

转载 作者:行者123 更新时间:2023-12-04 22:33:38 25 4
gpt4 key购买 nike

我的 WPF 应用程序中有一个如下所示的数据网格。

<Window x:Class="MyApp.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<DataGrid x:Name="dgTest" ItemsSource="{Binding TestSource}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTemplateColumn Width="125" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Column1}"></TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="500" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Column2}"></TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<Button Click="SaveButton_Click">Save</Button>
</Grid>
</Window>

我使用以下代码绑定(bind)它。现在我的要求是当用户在数据网格内的这些文本框中输入一些文本并单击保存按钮时,它应该更新数据库。我怎样才能做到这一点?
namespace MyApp
{
public partial class TestWindow: Window
{
private ObservableCollection<Test> _testSource
public ObservableCollection<Test> TestSource
{
get
{
return _testSource;
}
set
{
_testSource = value;
OnPropertyChanged("TestSource");
}
}

public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));
}
}

public TestWindow()
{
InitializeComponent();
TestSource= new ObservableCollection<Test>();
string strConnString = Application.Current.Properties["connectionStr"].ToString();
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand("SELECT Column1,Column2 FROM MyTable", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dtTest = new DataTable();
da.Fill(dtTest);
foreach (DataRow row in dtTest)
{
Test cd = new Test();
cd.Column1 = row["Column1"].ToString();
cd.Column2 = row["Column2"].ToString();
TestSource.Add(cd);
}
this.DataContext = this;
}

private void SaveButton_Click(object sender, RoutedEventArgs e)
{
// here I need to get the updated ObservableCollection, but now it is showing old data
foreach Test t in TestSource)
{
string a = t.Column1;
string b = t.Column2;
}
}
}

public class Test:
{
public string Column1{ get; set; }
public string Column2{ get; set; }
}
}

谢谢

最佳答案

DataGridTemplateColumn 中创建自己的 UI 时(或 custom DataGrid.RowStyle ),DataGrid 更改 UpdateSourceTrigger (即何时应该更新基础数据)在所有绑定(bind)到 Explicit 上如果您没有自己指定它们。

此处简要描述了该“功能”:5 Random Gotchas with the WPF DataGrid ,即使作者说无论您是否设置 UpdateSourceTrigger 都会发生这种情况自己,自己设置确实有效(至少在.Net 4.0中)。

使用LostFocus模仿默认 TextBox行为(和 PropertyChangedCheckBox 等):

...
<TextBox Text="{Binding Column1, UpdateSourceTrigger=LostFocus}"></TextBox>
...
<TextBox Text="{Binding Column2, UpdateSourceTrigger=LostFocus}"></TextBox>
...

关于wpf - WPF 中的 DataGrid 和 Observable 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14193507/

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