【问题标题】:Checkbox click copy texblocks in to a string list复选框单击将 texblocks 复制到字符串列表中
【发布时间】:2013-11-13 13:55:07
【问题描述】:

在 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>

【问题讨论】:

    标签: c# wpf treeview hierarchicaldatatemplate


    【解决方案1】:

    我目前无法对此进行测试,但我认为它应该工作:

    private void TreeView_Checked(object sender, RoutedEventArgs e)
    {
        CheckBox chkBox = sender as CheckBox;
        StackPanel stackPanel = chkBox.Parent as StackPanel;
        TextBlock txtBlock = FindVisualChild<TextBlock>(stackPanel);
        Hyperlink hyperlink = FindVisualChild<Hyperlink>(stackPanel);
        TextBlock secondTextBlock = FindVisualChild<TextBlock>(hyperlink);
    
        bool isChecked = chkBox.IsChecked.HasValue ? chkBox.IsChecked.Value : false;
    
        if (isChecked)
        {  
            selectedNames.Add(txtBlock.Text);   
            selectedNames.Add(secondTextBlock.Text);           
        }       
    }
    

    告诉我进展如何。


    更新>>>

    好的,所以我不确定这是否可行……看起来它仍然应该可行,但我会接受你说它不可行。因此,我能想到您获得第二个TextBlock 的唯一其他方法是给它一个名称并使用FindName 方法。

    我不会在这里复制所有内容,而是将您指向 MSDN 上的 How to: Find DataTemplate-Generated Elements 页面,该页面具有详细的代码示例和解释。基本上,您需要从相关的TreeViewItem 获取ContentPresenter,然后您可以从ContentPresenter 访问DataTemplate。最后使用DataTemplate,您可以像这样访问TextBlock

    首先更改您的 XAML:

    <Hyperlink NavigateUri="{Binding XPath=@WebSite}" 
        RequestNavigate="Hyperlink_RequestNavigate">  
        <TextBlock Name="SecondTextBlock" Text="{Binding XPath=@WebSite}" /> 
    </Hyperlink>
    

    然后获取ContentPresenter 并像这样使用它:

    TextBlock secondTextBlock = dataTemplate.
        FindName("SecondTextBlock", contentPresenter) as TextBlock;
    if (secondTextBlock != null) selectedNames.Add(secondTextBlock.Text );
    

    【讨论】:

    • 在 VisualTree 应用新的超链接文本块时出现空异常错误。
    • null 是哪个对象?
    • 我知道“超链接”对象为空。在几个节点中,只有超链接的某个节点具有值。例如:&lt;Solution WebSite ="http://www.dnpphoto.eu/"/&gt;
    • @Shreidan:我尝试了你的解决方案,但我仍然从超链接文本块中得到空白的命运。我不知道下一步该做什么?
    猜你喜欢
    • 2020-08-02
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多