gpt4 book ai didi

c# - 如何调用 UpdateSource() 以在 DataGrid 上进行显式绑定(bind)?

转载 作者:太空狗 更新时间:2023-10-30 01:03:05 24 4
gpt4 key购买 nike

我有一个 DataGrid,其中包含来自设置对象的信息。当前,DataGrid 和设置对象之间存在双向绑定(bind)。但是,我想放置一个“保存”按钮,如果用户单击“保存”按钮,它只会将 DataGrid 中所做的更改绑定(bind)到对象。但是,我不确定如何使用我的 DataGrid 为我的特定情况调用 UpdateSource()。

这是我的 xaml.cs 代码:

public void LoadDataFields(Data d)
{
Grid1.ItemsSource = d.Fields;
}

private void SaveChanges(object sender, RoutedEventArgs e)
{
BindingExpression be = Grid1.GetBindingExpression(DataGrid.ItemsSourceProperty);
be.UpdateSource();
}

这是我的 xaml 代码:

<DataGrid x:Name="Grid1" 
IsReadOnly="False"
Height="360"
Margin="20,15,20,15"
VerticalAlignment="Top"
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}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Length of Field">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=length, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

是否有一种简单的方法来调用 UpdateSource() 以便仅在单击“保存”按钮时才发生绑定(bind)?我的猜测是我只是为 GetBindingExpression 方法放入了错误的属性。

最佳答案

是的,有一种方法,但这不是一种非常非常简单的方法。首先,您需要 2 个辅助方法:

public static T GetVisualChild<T>(Visual parent) where T : Visual
{
Visual visual;
T child = default(T);

int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
visual = (Visual)VisualTreeHelper.GetChild(parent, i);
child = visual as T;
if (child == null)
{
child = GetVisualChild<T>(visual);
}
if (child != null)
{
break;
}
}
return child;
}

public T FindVisualChild<T>(DependencyObject obj, string name) where T : DependencyObject
{
DependencyObject child;
FrameworkElement frameworkElement;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
child = VisualTreeHelper.GetChild(obj, i);
frameworkElement = child as FrameworkElement;
if (child != null && child is T && frameworkElement != null && frameworkElement.Name == name)
{
return (T)child;
}
else
{
T childOfChild = FindVisualChild<T>(child, name);
if (childOfChild != null)
{
return childOfChild;
}
}
}

return null;
}

然后您必须为绑定(bind)的控件命名。例如“文本框”:

<DataGridTemplateColumn Header="Field">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="textBox" Text="{Binding Path=name, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Length of Field">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="textBox" Text="{Binding Path=length, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

然后在您的SaveChanges 方法中,您可以使用以下代码:

private void SaveChanges(object sender, RoutedEventArgs e)
{
DataGridRow dataGridRow;
DataGridCellsPresenter dataGridCellsPresenter;
DataGridCell dataGridCell;
TextBox textBox;
BindingExpression bindingExpression;

for (int i = 0; i < Grid1.Items.Count; i++)
{
dataGridRow = (DataGridRow)Grid1.ItemContainerGenerator.ContainerFromIndex(i);
dataGridCellsPresenter = GetVisualChild<DataGridCellsPresenter>(dataGridRow);
for (int j = 0; j < Grid1.Columns.Count; j++)
{
dataGridCell = (DataGridCell)dataGridCellsPresenter.ItemContainerGenerator.ContainerFromIndex(j);
textBox = FindVisualChild<TextBox>(dataGridCell, "textBox");
if (textBox != null)
{
bindingExpression = BindingOperations.GetBindingExpression(textBox, TextBox.TextProperty);
bindingExpression.UpdateSource();
}
}
}
}

希望对您有所帮助。

关于c# - 如何调用 UpdateSource() 以在 DataGrid 上进行显式绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30789890/

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