Find control inside Listbox.ItemTemplate (WPF C#)(在 Listbox.ItemTemplate (WPF C#) 中查找控件)
问题描述
我在 StackPanel
中找到正确的 TextBlock
控件时遇到了一些问题.我的标记:
I have some problems finding the right TextBlock
control inside a StackPanel
.
My markup:
<ListBox Name="lstTimeline" ItemContainerStyle="{StaticResource TwItemStyle}"
MouseDoubleClick="lstTimeline_MouseDoubleClick">
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel MaxWidth="{Binding ElementName=lstTimeline, Path=ActualWidth}">
<Border Margin="10" DockPanel.Dock="Left" BorderBrush="White"
BorderThickness="1" Height="48" Width="48" HorizontalAlignment="Center">
<Image Source="{Binding ThumbNail, IsAsync=True}" Height="48" Width="48" />
</Border>
<StackPanel Name="stkPanel" Margin="10" DockPanel.Dock="Right">
<TextBlock Text="{Binding UserName}" FontWeight="Bold" FontSize="18" />
<TextBlock Text="{Binding Text}" Margin="0,4,0,0" FontSize="14"
Foreground="#c6de96" TextWrapping="WrapWithOverflow" />
<TextBlock Text="{Binding ApproximateTime}" FontSize="14"
FontFamily="Georgia" FontStyle="Italic" Foreground="#BBB" />
<TextBlock Text="{Binding ScreenName}" Name="lblScreenName" FontSize="14"
FontFamily="Georgia" FontStyle="Italic" Foreground="#BBB"
Loaded="lblScreenName_Loaded" />
</StackPanel>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我的双击代码:
private void lstTimeline_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
ListBoxItem lbi = (lstTimeline.SelectedItem as ListBoxItem);
StackPanel item = lbi.FindName("stkPanel") as StackPanel;
if (item != null)
MessageBox.Show("StackPanel null");
TextBlock textBox = item.FindName("lblScreenName") as TextBlock;
if (textBox != null)
MessageBox.Show("TextBlock null");
MessageBox.Show(textBox.Text);
}
但 StackPanel
为空.如何在SelectedItem
中找到正确的TextBlock
?
But the StackPanel
is null. How do find the right TextBlock
in SelectedItem
?
感谢您的帮助.
推荐答案
ListBoxItem myListBoxItem = (ListBoxItem)(lstUniqueIds.ItemContainerGenerator.ContainerFromIndex(lstUniqueIds.SelectedIndex));
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
CheckBox target = (CheckBox)myDataTemplate.FindName("chkUniqueId", myContentPresenter);
if (target.IsChecked)
{
target.IsChecked = false;
}
else
{
target.IsChecked = true;
}
函数 FindVisualChild
可以在 MSDN 页面 FrameworkTemplate.FindName 方法:
Function FindVisualChild
can be found on the MSDN page FrameworkTemplate.FindName Method:
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;
}
这篇关于在 Listbox.ItemTemplate (WPF C#) 中查找控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Listbox.ItemTemplate (WPF C#) 中查找控件
基础教程推荐
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- MS Visual Studio .NET 的替代品 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01