在 HierarchicalDataTemplate 中,我得到了 2 textblocks
,我想从两者中复制文本。在复选框单击/检查期间,它会将文本更新为列表。
该功能正在从一个文本块中获取文本。
如何从两个文本块中获取文本并将其更新到列表中?
private List<string> selectedNames = new List<string>();
private void TreeView_Checked(object sender, RoutedEventArgs e)
{
CheckBox chkBox = sender as CheckBox;
StackPanel stackPanel = chkBox.Parent as StackPanel;
TextBlock txtBlock = FindVisualChild<TextBlock>(stackPanel);
bool isChecked = chkBox.IsChecked.HasValue ? chkBox.IsChecked.Value : false;
if (isChecked)
{
selectedNames.Add(txtBlock.Text );
}
}
CheckBox 获取文本功能:
private static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is T)
return (T)child;
else
{
T childOfChild = FindVisualChild<T>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
WPF 分层数据模板:
<StackPanel Orientation="Horizontal" >
<CheckBox Name="checkBoxTree" Checked="TreeView_Checked" Unchecked="checkBoxTree_Unchecked"
Margin="0,4,0,0" Style="{DynamicResource CheckBoxStyle1}"/>
<TextBlock Text="{Binding XPath=@Name, Mode=TwoWay}" />
<TextBlock >
<Hyperlink NavigateUri="{Binding XPath=@WebSite}" RequestNavigate="Hyperlink_RequestNavigate">
<TextBlock Text="{Binding XPath=@WebSite}" />
</Hyperlink>
</TextBlock>
</StackPanel>