gpt4 book ai didi

c# - Listview 中的 TextBox - 在 TextBox TextChanged 上获取 ListViewItem

转载 作者:太空宇宙 更新时间:2023-11-03 20:22:35 26 4
gpt4 key购买 nike

我有一个显示一些 TextBoxesListView。对于每个 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/

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