zoukankan      html  css  js  c++  java
  • 如何在WPF和Silverlight中取得DataTemplate中的命名元素

    WPF

    1.假如ListBox使用如下的DataTemplate

    代码
    <DataTemplate x:Key="myDataTemplate">
      
    <TextBlock Name="textBlock" FontSize="14" Foreground="Blue">
        
    <TextBlock.Text>
          
    <Binding XPath="Title"/>
        
    </TextBlock.Text>
      
    </TextBlock>
    </DataTemplate>

    2.在cs文件中写如下的代码可以找到某个ListBoxItem对应的TextBlock

    代码
    ListBoxItem myListBoxItem =
        (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));

    ContentPresenter myContentPresenter 
    = FindVisualChild<ContentPresenter>(myListBoxItem);

    DataTemplate myDataTemplate 
    = myContentPresenter.ContentTemplate;
    TextBlock myTextBlock 
    = (TextBlock)myDataTemplate.FindName("textBlock", myContentPresenter);
    MessageBox.Show(
    "The text of the TextBlock of the selected list item: "
        
    + myTextBlock.Text);

    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;
    }

    SilverLight

    1. Xmal 和WPF中相同

    2. 由于SL中的DataTemplate没有FindName方法,因此c#代码有所区别

    代码
    ListBoxItem myListBoxItem =
        (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));

    TextBlock myTextBlock 
    = FindFirstVisualChild<TextBlock>(_myListBoxItem , "textBlock");
    MessageBox.Show(
    "The text of the TextBlock of the selected list item: "
        
    + myTextBlock.Text);

    private childItem FindVisualChild<childItem>(DependencyObject obj,String childName)
        
    where childItem : DependencyObject
    {
        
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            DependencyObject child 
    = VisualTreeHelper.GetChild(obj, i);
            
    if (child != null && child is childItem && child.GetValue(NameProperty).ToString()==childName)
                
    return (childItem)child;
            
    else
            {
                childItem childOfChild 
    = FindVisualChild<childItem>(child,childName);
                
    if (childOfChild != null)
                    
    return childOfChild;
            }
        }
        
    return null;
    }
  • 相关阅读:
    ABP文档
    ABP框架没有httpPost,httpget,httpput特性
    使用命令关闭占用80端口的程序
    nginx安装编译详解
    docker端口映射设置
    centos6.3安装nginx
    process有个env属性,env属性就是环境变量,里面可以访问到NODE_ENV;NODE_ENV是在启动nodejs时添加上去的;
    使用nssm在windows服务器上部署nodejs
    对象相等与不相等
    可用的CSS文字两端对齐
  • 原文地址:https://www.cnblogs.com/xixifusigao/p/1656453.html
Copyright © 2011-2022 走看看