Difference between revisions of "WPF DataGrid with dynamic cols"

From no name for this wiki
Jump to: navigation, search
(MyTable.xaml)
(X)
Line 105: Line 105:
 
     }
 
     }
 
}
 
}
 
</source>
 
 
=== X  ===
 
<source lang="csharp">
 
  
 
</source>
 
</source>

Revision as of 10:09, 18 June 2016

X


MainWindow.xaml

<Window x:Class="ControlsTestProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ControlsTestProject"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:TableVm x:Key="ViewModel"/>
    </Window.Resources>
    
    <StackPanel DataContext="{Binding Source={StaticResource ViewModel}}">

        <local:MyTable />

    </StackPanel>
</Window>


MyTable.xaml

<UserControl x:Class="ControlsTestProject.MyTable"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:ControlsTestProject"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">



    <Grid>
        
        <DataGrid x:Name="MyDataGrid" 
                  ItemsSource="{Binding Path=Rows}" 
                  AutoGenerateColumns="False"
                  CanUserAddRows="False">
            
        </DataGrid>   
    </Grid>
</UserControl>

MyTable.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;


namespace ControlsTestProject
{
    /// <summary>
    /// Interaction logic for MyTable.xaml
    /// </summary>
    public partial class MyTable : UserControl
    {
        public MyTable()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MyControl_Loaded);
        }

        private void GenerateColumns()
        {

        }

        void MyControl_Loaded(object sender, RoutedEventArgs e)
        {
            TableVm tableVm = (TableVm) MyDataGrid.DataContext;
            int numCols = tableVm.NumCols();
            for(int i=0; i<numCols; i++)
            {
                DataGridTemplateColumn template = CreateTemplatedColumn(i);
                MyDataGrid.Columns.Add(template);
            }
        }

        private DataGridTemplateColumn CreateTemplatedColumn(int col)
        {
            DataGridTemplateColumn result = new DataGridTemplateColumn();
            FrameworkElementFactory factory = new FrameworkElementFactory(typeof(MyCellPresenter));

            Binding binding = new Binding();
            string bindingPath = string.Format(@"Cells[{0}]", col);
            binding.Path = new PropertyPath(bindingPath);
            factory.SetBinding(DataContextProperty, binding);

            result.CellTemplate = new DataTemplate { VisualTree = factory };
            return result;
        }
    }
}

MyCellPresenter.xaml

<UserControl x:Class="ControlsTestProject.MyCellPresenter"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:ControlsTestProject"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.Resources>
        
        <DataTemplate x:Key="Template1">
            <local:MyControl1/>
        </DataTemplate>

        <DataTemplate x:Key="Template2">
            <local:MyControl2/>
        </DataTemplate>

        <local:MyTemplateSelector x:Key="TemplateSelector"/>

    </UserControl.Resources>
    
    <StackPanel Background="Yellow">
        
        <Label  Foreground="Black"
          Content="{Binding}"
          ContentTemplateSelector="{StaticResource TemplateSelector}">
        </Label>
        
    </StackPanel>
</UserControl>

MyControl1.xaml

<UserControl x:Class="ControlsTestProject.MyControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:ControlsTestProject"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBlock Text="{Binding Path=Text}" 
                   PreviewMouseLeftButtonDown="TextBlock_PreviewMouseLeftButtonDown">
            <TextBlock.Background>
                <SolidColorBrush>
                    <SolidColorBrush.Color>
                        Aquamarine
                    </SolidColorBrush.Color>
                </SolidColorBrush>
            </TextBlock.Background>
        </TextBlock>
    </Grid>
</UserControl>


MyControl2.xaml

<UserControl x:Class="ControlsTestProject.MyControl2"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:ControlsTestProject"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             d:DataContext="{d:DesignInstance Type=local:MyItemVm}">
    <StackPanel Background="Green">
        <TextBox Text="{Binding Path=Text}"/>       
    </StackPanel>
</UserControl>

X