WPF DataGrid with dynamic cols
Contents
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>
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>