zoukankan      html  css  js  c++  java
  • DataTemplate.LoadContent Method将resource中的datatemplate转换为UIElement,可以用于对象添加

    https://msdn.microsoft.com/zh-cn/library/windows/apps/windows.ui.xaml.datatemplate.loadcontent.aspx

    <StackPanel  Name="rootStackPanel">
    
      <StackPanel.Resources>
        <DataTemplate x:Key="oddNumberTemplate">
          <Grid>
            <Rectangle Stroke="Purple" StrokeThickness="4" />
            <TextBlock HorizontalAlignment="Center" 
                       VerticalAlignment="Center" 
                       FontSize="24" Foreground="Blue" 
                       FontWeight="Bold"/>
          </Grid>
        </DataTemplate>
    
    
        <DataTemplate x:Key="evenNumberTemplate">
          <Grid>
            <Ellipse Stroke="Green" StrokeThickness="4"/>
            <TextBlock HorizontalAlignment="Center" 
                       VerticalAlignment="Center" 
                       FontSize="24" Foreground="Red" 
                       FontWeight="Bold"  />
          </Grid>
        </DataTemplate>
      </StackPanel.Resources>
    
      <Border Name="selectedItemDisplay"
              Width="50" Height="50"/>
    
      <ListBox Name="numberList" SelectionChanged="ListBox_SelectionChanged">
        <ListBoxItem Content="1"/>
        <ListBoxItem Content="2"/>
        <ListBoxItem Content="3"/>
        <ListBoxItem Content="4"/>
        <ListBoxItem Content="5"/>
        <ListBoxItem Content="6"/>
        <ListBoxItem Content="7"/>
        <ListBoxItem Content="8"/>
        <ListBoxItem Content="9"/>
        <ListBoxItem Content="10"/>
      </ListBox>
    
    </StackPanel>
    private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
        SelectDataTemplate(lbi.Content);
    }
    
    private void SelectDataTemplate(object value)
    {
        string numberStr = value as string;
    
        if (numberStr != null)
        {
            int num;
    
            try
            {
                num = Convert.ToInt32(numberStr);
            }
            catch
            {
                return;
            }
    
            DataTemplate template;
    
            // Select one of the DataTemplate objects, based on the 
            // value of the selected item in the ComboBox.
            if (num % 2 != 0)
            {
                template = rootStackPanel.Resources["oddNumberTemplate"] as DataTemplate;
            }
            else
            {
                template = rootStackPanel.Resources["evenNumberTemplate"] as DataTemplate;
            }
    
            selectedItemDisplay.Child = template.LoadContent() as UIElement;
            TextBlock tb = FindVisualChild<TextBlock>(selectedItemDisplay);
            tb.Text = numberStr;
        }
    }
    
    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;
    }
    
  • 相关阅读:
    U盘禁用工具1.3
    《中文专业论文写作概论》笔记
    基于WAP1.0的手机论坛网站系统
    销售统计SQL
    移动如何保护个人开发者的合法权益?
    c#使用winnet检测网络连接状况
    HBASE客户端的一些参数设置
    代理模式
    使用Eclipse+Axis2生成webservice
    java多线程的理解
  • 原文地址:https://www.cnblogs.com/xlyg-14/p/4763592.html
Copyright © 2011-2022 走看看