none
DataTemplateSelector选择判断对象问题 RRS feed

  • 问题

  • xaml代码

    <Window x:Class="test.PowerSupplySystem"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="PowerSupplySystem" Height="314" Width="476" 
            xmlns:dts="clr-namespace:test">
        <Window.Resources>
            <DataTemplate x:Key="SwitchDataTemplate">
              <StackPanel Margin="3">
                    <TextBox Text="{Binding Mswitch}"/>
                    <Image Source="/IMAGE/断路器.bmp"/>
                    </StackPanel>
            </DataTemplate>
    
            <DataTemplate x:Key="cableDataTemplate">
                <StackPanel Margin="3">
                    <TextBox Text="{Binding Mswitch}"/>
                    <Image Source="/IMAGE/电缆.bmp"/>
                </StackPanel>
            </DataTemplate>
    
            
             
    
        </Window.Resources>
    
        <Grid>
            <DataGrid Name="system" Loaded="Window_Loaded" AutoGenerateColumns="False" >
                <DataGrid.Columns >
                    <DataGridTemplateColumn Width="150" Header="开关"   />
                    <DataGridTextColumn Width="300" Header="电缆"  Binding="{Binding Mcable}" />
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
        </Window>
    

    C#代码

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using BLL;
    
    namespace test
    {
        /// <summary>
        /// PowerSupplySystem.xaml 的交互逻辑
        /// </summary>
        public partial class PowerSupplySystem : Window
        {
            public PowerSupplySystem()
            {
                InitializeComponent();
            }
    
           
            //启动时加载
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                system.ItemsSource = PowerSupplySystemBll.GetAll();
    
            }
    
        }
    
        public class TaskListDataTemplateSelector : DataTemplateSelector
        {
            public override DataTemplate
                SelectTemplate(object item, DependencyObject container)
            {
                FrameworkElement element = container as FrameworkElement;
    
                if (element != null && item != null && item is System)
                {
                    Task taskitem = item as Task;
    
                    if (System.内容!=null)
                        return
                            element.FindResource("SwitchDataTemplate") as DataTemplate;
                    else
                        return
                            element.FindResource("cableDataTemplate") as DataTemplate;
                }
    
                return null;
            }
        }

    这个项目我想采用模板控制器,通过判断datagrid里面Columns是否有内容,作为依据选择模板。

    现在想问的是怎么才能在C#里面调用到这个datagrid的内容?


    2017年9月28日 9:26

答案


  • Hi  yzjjhk,

    >>这个项目我想采用模板控制器,通过判断datagrid里面Columns是否有内容,作为依据选择模板。

    请参考下面的例子来实现。

    XAML:

     <Window.Resources>
            <DataTemplate x:Key="SwitchDataTemplate">
                <StackPanel Margin="3">
                    <TextBox Text="{Binding FirstName}"/>
                    <Image Source="\images\bac.JPG"/>
                </StackPanel>
            </DataTemplate>
    
            <DataTemplate x:Key="cableDataTemplate">
                <StackPanel Margin="3">
                    <TextBox Text="{Binding FirstName}"/>
                  
                    <Image Source="\images\Gakuse.ico"/>
                </StackPanel>
            </DataTemplate>
        </Window.Resources>
    
        <Grid>
            <DataGrid ItemsSource="{Binding}" Name="system" Loaded="Window_Loaded" AutoGenerateColumns="False" >
                <DataGrid.Columns >
                    <DataGridTemplateColumn  Width="150" Header="开关">
                        <DataGridTemplateColumn.CellTemplateSelector>
                            <local:TestStudentTemplateSelector 
                                SwitchDataTemplate="{StaticResource SwitchDataTemplate}"
                                cableDataTemplate="{StaticResource cableDataTemplate}"/>
                        </DataGridTemplateColumn.CellTemplateSelector>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn Width="300" Header="电缆"  Binding="{Binding FirstName}" />
                </DataGrid.Columns>
            </DataGrid>
        </Grid>

    XAML.CS:

    public partial class DataTemplateSelectordEMO2 : Window
        {
    
            ObservableCollection<TestStudent> teststudents = new ObservableCollection<TestStudent>();
    
    
            public DataTemplateSelectordEMO2()
            {
                InitializeComponent();
    
                teststudents.Add(new TestStudent(10, "Bob"));
                teststudents.Add(new TestStudent(25, "James"));
                teststudents.Add(new TestStudent(15, "Joe"));
                teststudents.Add(new TestStudent(12, "Dona"));
                teststudents.Add(new TestStudent(18, "Peter"));
                DataContext = teststudents;
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
    
    
    
    
            }
        }
    
    
        public class TestStudentTemplateSelector : DataTemplateSelector
        {
            public DataTemplate SwitchDataTemplate
            { get; set; }
    
            public DataTemplate cableDataTemplate
    
            { get; set; }
    
            public override DataTemplate SelectTemplate(object item, DependencyObject container)
            {
                TestStudent student = item as TestStudent;
                if (student != null)
                {
                    if (student.ID >= 15)
                        return SwitchDataTemplate;
                    else
                        return cableDataTemplate;
                }
                else
                    return base.SelectTemplate(item, container);
            }
    
        }
    
        public class TestStudent
        {
            public TestStudent(int id, String firstName )
            {
                ID = id;
                FirstName = firstName;
            }
    
            public int ID
            { get; set; }
    
            public String FirstName
            { get; set; }
    
        }


    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 yzjjhk 2017年10月2日 14:41
    2017年9月29日 7:02
    版主

全部回复

  • 具体想实现的功能就是,根据单元格里面的数据选择模板,当有数据时,加载一个模板,没有数据时加载另外一个模板


    • 已编辑 yzjjhk 2017年9月28日 15:36
    2017年9月28日 15:32

  • Hi  yzjjhk,

    >>这个项目我想采用模板控制器,通过判断datagrid里面Columns是否有内容,作为依据选择模板。

    请参考下面的例子来实现。

    XAML:

     <Window.Resources>
            <DataTemplate x:Key="SwitchDataTemplate">
                <StackPanel Margin="3">
                    <TextBox Text="{Binding FirstName}"/>
                    <Image Source="\images\bac.JPG"/>
                </StackPanel>
            </DataTemplate>
    
            <DataTemplate x:Key="cableDataTemplate">
                <StackPanel Margin="3">
                    <TextBox Text="{Binding FirstName}"/>
                  
                    <Image Source="\images\Gakuse.ico"/>
                </StackPanel>
            </DataTemplate>
        </Window.Resources>
    
        <Grid>
            <DataGrid ItemsSource="{Binding}" Name="system" Loaded="Window_Loaded" AutoGenerateColumns="False" >
                <DataGrid.Columns >
                    <DataGridTemplateColumn  Width="150" Header="开关">
                        <DataGridTemplateColumn.CellTemplateSelector>
                            <local:TestStudentTemplateSelector 
                                SwitchDataTemplate="{StaticResource SwitchDataTemplate}"
                                cableDataTemplate="{StaticResource cableDataTemplate}"/>
                        </DataGridTemplateColumn.CellTemplateSelector>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn Width="300" Header="电缆"  Binding="{Binding FirstName}" />
                </DataGrid.Columns>
            </DataGrid>
        </Grid>

    XAML.CS:

    public partial class DataTemplateSelectordEMO2 : Window
        {
    
            ObservableCollection<TestStudent> teststudents = new ObservableCollection<TestStudent>();
    
    
            public DataTemplateSelectordEMO2()
            {
                InitializeComponent();
    
                teststudents.Add(new TestStudent(10, "Bob"));
                teststudents.Add(new TestStudent(25, "James"));
                teststudents.Add(new TestStudent(15, "Joe"));
                teststudents.Add(new TestStudent(12, "Dona"));
                teststudents.Add(new TestStudent(18, "Peter"));
                DataContext = teststudents;
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
    
    
    
    
            }
        }
    
    
        public class TestStudentTemplateSelector : DataTemplateSelector
        {
            public DataTemplate SwitchDataTemplate
            { get; set; }
    
            public DataTemplate cableDataTemplate
    
            { get; set; }
    
            public override DataTemplate SelectTemplate(object item, DependencyObject container)
            {
                TestStudent student = item as TestStudent;
                if (student != null)
                {
                    if (student.ID >= 15)
                        return SwitchDataTemplate;
                    else
                        return cableDataTemplate;
                }
                else
                    return base.SelectTemplate(item, container);
            }
    
        }
    
        public class TestStudent
        {
            public TestStudent(int id, String firstName )
            {
                ID = id;
                FirstName = firstName;
            }
    
            public int ID
            { get; set; }
    
            public String FirstName
            { get; set; }
    
        }


    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 yzjjhk 2017年10月2日 14:41
    2017年9月29日 7:02
    版主