XAML:
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Persons}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" />
<DataGridTextColumn Binding="{Binding Name}" />
<DataGridTemplateColumn Header="Name1" CellEditingTemplate ="{Binding Path=Cars, Converter={StaticResource myConverter}}" />
</DataGrid.Columns>
</DataGrid>
Converter:
public class myConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is ObservableCollection<Car> test)
{
//create the data template
DataTemplate cardLayout = new DataTemplate();
foreach (var t in test)
{
//set up the stack panel
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(StackPanel));
spFactory.Name = "myComboFactory";
spFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
//set up the textblock
FrameworkElementFactory childFactory;
childFactory = new FrameworkElementFactory(typeof(TextBlock));
childFactory.SetValue(TextBlock.TextProperty, t.Color);
spFactory.AppendChild(childFactory);
//set up the textblock
childFactory = new FrameworkElementFactory(typeof(TextBlock));
childFactory.SetValue(TextBlock.TextProperty, t.Speed);
spFactory.AppendChild(childFactory);
//set the visual tree of the data template
cardLayout.VisualTree = spFactory;
}
return cardLayout;
}
return DependencyProperty.UnsetValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
//Will do.
return DependencyProperty.UnsetValue;
}
}
C#:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public ObservableCollection<Car> Cars { get; set; }
}
public class Car
{
public string Color { get; set; }
public int Speed { get; set; }
}
public class ViewModelsWindow2
{
public ObservableCollection<Person> Persons { get; }
public ViewModelsWindow2()
{
Persons = new ObservableCollection<Person>();
Persons.Add(new Person() { Id = 177, Name = "John", Cars = new ObservableCollection<Car>() { new Car() { Color = "Green", Speed = 360 } } });
}
}
I want to create own DataGridTemplateColumn with some dynamic TextBoxes (at example two TextBlocks). I want to do it over converter because I need twoways of DataGridTemplateColumn. I haven't still written code for ConvertBack. Number of TextBoxes will be different at each row.
But I have one problem. The converter doesn't execute by WPF. I set up the breakpoint at the begin of Convert and the code has not stopped.
Please, help me to decide this question. What do I do wrong?
It's like this. It's static but I want dynamic.
I will perhaps add properties to Car and the person perhaps has two, three... cars with different colour and speed.
And the data will edit by user.
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Persons}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" />
<DataGridTextColumn Binding="{Binding Name}" />
<DataGridTemplateColumn Header="Name1" CellEditingTemplate ="{Binding Path=Cars, Converter={StaticResource myConverter}}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Text="Green"/>
<TextBox Text="360"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBox Text="Red"/>
<TextBox Text="360"/>
...
</StackPanel>
...
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Update:
I define Converter as:
xmlns:vm="clr-namespace:WpfApp1.ViewModels"
...
<Window.Resources>
<ResourceDictionary>
<vm:myConverter x:Key="myConverter" />
</ResourceDictionary>
</Window.Resources>
<Window.DataContext>
<vm:ViewModelsWindow2 />
</Window.DataContext>