我有一个显示一些 TextBoxes
的 ListView
。对于每个 TextBox
,我都会捕获一个 SelectionChanged
事件。
我的 XAML 文件看起来像这样:
<ListView>
<GridView>
...
<DataTemplate>
<TextBox SelectionChanged="Box_SelectionChanged" />
</DataTemplate>
...
</GridView>
</ListView>
我的 Xaml CS 文件如下所示:
private void Box_SelectionChanged(object sender, TextChangedEventArgs e) {}
在我的 Box_SelectionChanged
函数中,我想获取其中更新了文本框的 ListViewItem
。
我该怎么做?
最佳答案
你可以试试这个:
将此方法添加到您的类中:
public static T FindVisualParent<T>(UIElement element) where T : UIElement
{
UIElement parent = element; while (parent != null)
{
T correctlyTyped = parent as T; if (correctlyTyped != null)
{
return correctlyTyped;
}
parent = VisualTreeHelper.GetParent(parent) as UIElement;
} return null;
}
然后在 Box_SelectionChanged 事件处理程序中调用此方法:
private void Box_SelectionChanged(object sender, RoutedEventArgs e)
{
var tmp = FindVisualParent<ListViewItem>(sender as TextBox);
}
关于c# - Listview 中的 TextBox - 在 TextBox TextChanged 上获取 ListViewItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12608641/