gpt4 book ai didi

wpf - 如何访问数据网格模板列文本框文本 WPF C#

转载 作者:行者123 更新时间:2023-12-04 10:08:16 25 4
gpt4 key购买 nike

我需要访问 DataGrid 中的文本来自代码后面的模板列,但我不知道如何。我需要将文本更改为我在 SelectionChanged 上传递给它的任何字符串事件。有人可以告诉我如何实现这一目标吗?我发现了一个类似的问题 here
但它没有答案。

最佳答案

DataGrid 中查找控件模板列,你应该使用 FindChild() :

    public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
{
if (parent == null)
{
return null;
}

T foundChild = null;

int childrenCount = VisualTreeHelper.GetChildrenCount(parent);

for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
T childType = child as T;

if (childType == null)
{
foundChild = FindChild<T>(child, childName);

if (foundChild != null) break;
}
else
if (!string.IsNullOrEmpty(childName))
{
var frameworkElement = child as FrameworkElement;

if (frameworkElement != null && frameworkElement.Name == childName)
{
foundChild = (T)child;
break;
}
else
{
foundChild = FindChild<T>(child, childName);

if (foundChild != null)
{
break;
}
}
}
else
{
foundChild = (T)child;
break;
}
}

return foundChild;
}

例如,我在 中有这个模板列MyDataGrid :
<DataGridTemplateColumn Width="1.5*" IsReadOnly="False">
<DataGridTemplateColumn.Header>
<TextBlock Text="Sample" ToolTip="{Binding Path=Text, RelativeSource={RelativeSource Self}}" FontSize="14" />
</DataGridTemplateColumn.Header>

<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="MyTextBlock" Text="Hello!" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

从代码访问它,您可以:
TextBlock MyTextBlock = FindChild<TextBlock>(MyDataGrid, "MyTextBlock");

MessageBox.Show(MyTextBlock.Text);

注:只有在控件完全加载时才始终使用 FindChild,否则它将找不到它并返回 null。在本例中,我将此代码放在事件 中内容渲染 (Window) 表示窗口的所有内容都成功加载(即使事件 MyDataGrid_Loaded 也无法访问 MyTextBlock,因为它还没有加载):
    private void Window_ContentRendered(object sender, EventArgs e)
{
TextBlock MyTextBlock = FindChild<TextBlock>(MyDataGrid, "MyTextBlock");

MessageBox.Show(MyTextBlock.Text);
}

编辑 1:

访问选中行的控件添加事件SelectionChanged到 数据网格 在其中起作用,这将给出一个选定的行:
    private void MyDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
var row_list = GetDataGridRows(MyDataGrid);

foreach (DataGridRow single_row in row_list)
{
if (single_row.IsSelected == true)
{
TextBlock MyTextBlock = FindChild<TextBlock>(single_row, "MyTextBlock");

MessageBox.Show(MyTextBlock.Text);
}
}
}

catch
{
throw new Exception("Can't get access to DataGridRow");
}
}

GetDataGridRows() 列表:
    public IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid)
{
var itemsSource = grid.ItemsSource as IEnumerable;

if (null == itemsSource)
{
yield return null;
}

foreach (var item in itemsSource)
{
var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;

if (null != row)
{
yield return row;
}
}
}

编辑2:

为了获得所有项目,我重写了函数 FindChild():
    public static void FindChildGroup<T>(DependencyObject parent, string childName, ref List<T> list) where T : DependencyObject
{
// Checks should be made, but preferably one time before calling.
// And here it is assumed that the programmer has taken into
// account all of these conditions and checks are not needed.
//if ((parent == null) || (childName == null) || (<Type T is not inheritable from FrameworkElement>))
//{
// return;
//}

int childrenCount = VisualTreeHelper.GetChildrenCount(parent);

for (int i = 0; i < childrenCount; i++)
{
// Get the child
var child = VisualTreeHelper.GetChild(parent, i);

// Compare on conformity the type
T child_Test = child as T;

// Not compare - go next
if (child_Test == null)
{
// Go the deep
FindChildGroup<T>(child, childName, ref list);
}
else
{
// If match, then check the name of the item
FrameworkElement child_Element = child_Test as FrameworkElement;

if (child_Element.Name == childName)
{
// Found
list.Add(child_Test);
}

// We are looking for further, perhaps there are
// children with the same name
FindChildGroup<T>(child, childName, ref list);
}
}

return;
}

调用这个新函数:
   private void Window_ContentRendered(object sender, EventArgs e)
{
// Create the List
List<TextBlock> list = new List<TextBlock>();

// Find all elements
FindChildGroup<TextBlock>(MyDataGrid, "MyTextBlock", ref list);
string text = "";

// Print
foreach (TextBlock elem in list)
{
text += elem.Text + "\n";
}

MessageBox.Show(text, "Text in TextBlock");
}

一般来说,这种做法不是最好的......要获取项目(例如全部或选定),您可以直接联系存储您数据的列表(例如 ObservableCollection )。此外,它是有用的事件,例如 PropertyChanged。

关于wpf - 如何访问数据网格模板列文本框文本 WPF C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16997951/

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