DataGridColumn header 中的 ComboBox - 如何区分列?
ComboBox in the header of a DataGridColumn - how to differentiate between columns?
我在 DataGrid
列的 header 中有一个 ComboBox
。我想处理 ComboBox
的 SelectionChanged
事件,但我需要知道哪个控件(在哪一列的 header 中)生成了该事件。控件通过调用列构造函数时分配给 HeaderTemplate
的静态资源 DataTemplate
放入 header 中。
我想使用列数据上下文在 ComboBox
上设置一些标识数据,但我无法访问该上下文。我可以轻松访问 DataGrid
数据模型上下文,但它是所有 ComboBoxes
(列)的相同数据。
知道如何解决哪个列 header ComboBox
生成了事件吗?
"which column header ComboBox generated the event?"
(ComboBox)sender
是生成事件的 ComboBox 的句柄。
如果您需要访问 header 或包含该 ComboBox 的列,您可以使用 VisualTreeHelper,如下所述:How can I find WPF controls by name or type?
根据您的问题中的信息,该线程的这个答案可能是您正在寻找的(作者:John Myczek)- 为您想要的类型分出 Window
:
You can use the VisualTreeHelper to find controls. Below is a method
that uses the VisualTreeHelper to find a parent control of a specified
type. You can use the VisualTreeHelper to find controls in other ways
as well.
public static class UIHelper
{
/// <summary>
/// Finds a parent of a given item on the visual tree.
/// </summary>
/// <typeparam name="T">The type of the queried item.</typeparam>
/// <param name="child">A direct or indirect child of the queried item.</param>
/// <returns>The first parent item that matches the submitted type parameter.
/// If not matching item can be found, a null reference is being returned.</returns>
public static T FindVisualParent<T>(DependencyObject child)
where T : DependencyObject
{
// get parent item
DependencyObject parentObject = VisualTreeHelper.GetParent(child);
// we’ve reached the end of the tree
if (parentObject == null) return null;
// check if the parent matches the type we’re looking for
T parent = parentObject as T;
if (parent != null)
{
return parent;
}
else
{
// use recursion to proceed with next level
return FindVisualParent<T>(parentObject);
}
}
}
Call it like this:
Window owner = UIHelper.FindVisualParent<Window>(myControl);
我在 DataGrid
列的 header 中有一个 ComboBox
。我想处理 ComboBox
的 SelectionChanged
事件,但我需要知道哪个控件(在哪一列的 header 中)生成了该事件。控件通过调用列构造函数时分配给 HeaderTemplate
的静态资源 DataTemplate
放入 header 中。
我想使用列数据上下文在 ComboBox
上设置一些标识数据,但我无法访问该上下文。我可以轻松访问 DataGrid
数据模型上下文,但它是所有 ComboBoxes
(列)的相同数据。
知道如何解决哪个列 header ComboBox
生成了事件吗?
"which column header ComboBox generated the event?"
(ComboBox)sender
是生成事件的 ComboBox 的句柄。
如果您需要访问 header 或包含该 ComboBox 的列,您可以使用 VisualTreeHelper,如下所述:How can I find WPF controls by name or type?
根据您的问题中的信息,该线程的这个答案可能是您正在寻找的(作者:John Myczek)- 为您想要的类型分出 Window
:
You can use the VisualTreeHelper to find controls. Below is a method that uses the VisualTreeHelper to find a parent control of a specified type. You can use the VisualTreeHelper to find controls in other ways as well.
public static class UIHelper { /// <summary> /// Finds a parent of a given item on the visual tree. /// </summary> /// <typeparam name="T">The type of the queried item.</typeparam> /// <param name="child">A direct or indirect child of the queried item.</param> /// <returns>The first parent item that matches the submitted type parameter. /// If not matching item can be found, a null reference is being returned.</returns> public static T FindVisualParent<T>(DependencyObject child) where T : DependencyObject { // get parent item DependencyObject parentObject = VisualTreeHelper.GetParent(child); // we’ve reached the end of the tree if (parentObject == null) return null; // check if the parent matches the type we’re looking for T parent = parentObject as T; if (parent != null) { return parent; } else { // use recursion to proceed with next level return FindVisualParent<T>(parentObject); } } }
Call it like this:
Window owner = UIHelper.FindVisualParent<Window>(myControl);