我有一个ListView显示一些TextBoxes.对于每个人TextBox,我都在捕捉一个SelectionChanged事件.
我的XAML文件看起来像这样:
<ListView>
<GridView>
...
<DataTemplate>
<TextBox SelectionChanged="Box_SelectionChanged" />
</DataTemplate>
...
</GridView>
</ListView>
Run Code Online (Sandbox Code Playgroud)
我的Xaml CS文件如下所示:
private void Box_SelectionChanged(object sender, TextChangedEventArgs e) {}
Run Code Online (Sandbox Code Playgroud)
在我的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;
}
Run Code Online (Sandbox Code Playgroud)
并在Box_SelectionChanged事件处理程序中调用此方法:
private void Box_SelectionChanged(object sender, RoutedEventArgs e)
{
var tmp = FindVisualParent<ListViewItem>(sender as TextBox);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1890 次 |
| 最近记录: |