我是靠谱客的博主 沉静时光,这篇文章主要介绍wpf 虚拟树获取listbox的usercontrol,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<Window x:Class="WpfExpand.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfExpand" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <StackPanel Orientation="Horizontal"> <ListBox x:Name="d2" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel Name="p1" Margin="10" DockPanel.Dock="Right"> <TextBlock Text="{Binding Name}" FontWeight="Bold" FontSize="18" /> <TextBlock Text="{Binding age}" Margin="0,4,0,0" FontSize="14" Foreground="#c6de96" TextWrapping="WrapWithOverflow" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </StackPanel> </Grid> </Window>
复制代码
1
2
3
4
5
6
7
8
9
10
this.Dispatcher.InvokeAsync(new Action(delegate { ListBoxItem myListBoxItem = (ListBoxItem)(d2.ItemContainerGenerator.ContainerFromIndex(0)); ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem); DataTemplate myDataTemplate = myContentPresenter.ContentTemplate; StackPanel target = (StackPanel)myDataTemplate.FindName("p1", myContentPresenter); }));

FindVisualChild方法

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { DependencyObject child = VisualTreeHelper.GetChild(obj, i); if (child != null && child is childItem) return (childItem)child; else { childItem childOfChild = FindVisualChild<childItem>(child); if (childOfChild != null) return childOfChild; } } return null; }

最后

以上就是沉静时光最近收集整理的关于wpf 虚拟树获取listbox的usercontrol的全部内容,更多相关wpf内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(77)

评论列表共有 0 条评论

立即
投稿
返回
顶部