Difference between revisions of "WPF DataGrid with dynamic cols"

From no name for this wiki
Jump to: navigation, search
(X)
(X)
Line 54: Line 54:
  
  
=== X ===
+
=== MyCellPresenter.xaml ===
 
<source lang="xml">
 
<source lang="xml">
 +
<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>
 
</source>
 
</source>
 
  
 
=== X  ===
 
=== X  ===

Revision as of 10:06, 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>


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>

X

X