Datagrid 自动生成的列自定义模板绑定
Datagrid autogenerated columns custom template binding
我正在尝试将 DataTemplate 应用于我自动生成的列。我可以用一个简单的模板来做到这一点。但是我想要一个组合框,其中文本绑定到数据网格,椭圆显示基于此值的颜色,通过转换器。
我试过两种方法。首先在代码隐藏中创建模板并通过 xamlReader.
加载它
private void LeftPanel_AutoGeneratingColumn(
object sender, DataGridAutoGeneratingColumnEventArgs e)
{
DataGridTemplateColumn col = new DataGridTemplateColumn();
col.Header = e.Column.Header;
string xaml =
"<DataTemplate xmlns:local=\"clr-namespace:myView\">" +
"<DataTemplate.Resources><local:goColorConverter x:Key=\"goColorConverter\" /></DataTemplate.Resources>" +
"<ComboBox " +
"SelectedValue=\"{Binding [" + e.Column.Header +
"], Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}\" Width=\"85\" " +
"ItemsSource=\"{Binding Path=DataContext.goNoGo, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}\">" +
"<ComboBox.ItemTemplate><DataTemplate><StackPanel Orientation=\"Horizontal\">" +
"<Ellipse Fill=\"{Binding Converter={StaticResource goColorConverter}}\" Height=\"14\" Width=\"14\" HorizontalAlignment=\"Right\"/>" +
"<TextBlock Text=\"{Binding}\" Padding=\"5 0\"/>" +
"</StackPanel></DataTemplate></ComboBox.ItemTemplate></ComboBox></DataTemplate>";
var sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml));
var pc = new ParserContext();
pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
var datatemplate = (DataTemplate)XamlReader.Load(sr, pc);
col.CellTemplate = datatemplate;
col.CellEditingTemplate = datatemplate;
e.Column = col;
return;
}
这为我提供了对数据的正确绑定,但我无法使用转换器进行编译。我得到错误无法为我的转换器创建未知类型(存在于我后面的代码中)。我已经阅读了很多关于程序集的帖子,但无法使用它们来帮助我。
我的第二种方法是在 xaml 中定义模板并将其应用到我后面的代码中。这使转换器工作,但我不知道如何定义我选择的值绑定,因为我不知道执行前的列名。我希望使用这个 Josh smith FindName example 但我不知道如何访问内容展示器。
代码:
private void LeftPanel_AutoGeneratingColumn(
object sender, DataGridAutoGeneratingColumnEventArgs e)
{
DataGridTemplateColumn col = new DataGridTemplateColumn();
col.Header = e.Column.Header;
col.CellTemplate = (DataTemplate)Resources["goDropColumn"];
col.CellEditingTemplate = (DataTemplate)Resources["goDropColumn"];
//Do magic here to get combo box and update its binding
e.Column = col;
return;
}
xaml:
<DataTemplate x:Key="goDropColumn">
<ComboBox Name="combo" Width="85" ItemsSource="{Binding Path=DataContext.goNoGo,
RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
SelectedValue="{Binding ???}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Ellipse Fill="{Binding Converter={StaticResource goColorConverter}}"
Height="14" Width="14"/>
<TextBlock Text="{Binding}" Padding="5 0"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
我不喜欢我的方法并且是 WPF 和 c# 的新手所以如果方法完全错误请告诉我。我觉得第二种方法更简洁,如果我可以更改标有 ??? 的绑定,它会起作用。到列名。
Another possibly relevant article 但我没有自定义数据类型,我只想将 selectedValue 绑定到该列。
根据 MachineLearning 的要求更新了代码:
DataGridTemplateColumn col = new DataGridTemplateColumn();
col.Header = e.Column.Header;
string xaml =
@"<DataTemplate x:Key=""goDropColumn""
xmlns:local=""clr-namespace:RP_SIL.View;assembly=RP_SIL.View""
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x = ""http://schemas.microsoft.com/winfx/2006/xaml"">
<ComboBox SelectedValue=""{Binding [" + e.Column.Header + @"], Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}""
Width=""85""
ItemsSource=""{Binding Path=DataContext.goNoGo, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"">
<ComboBox.Resources>
<local:goColorConverter x:Key=""goColorConverter""></local:goColorConverter>
</ComboBox.Resources>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation=""Horizontal"">
<Ellipse Fill=""{Binding Converter={StaticResource goColorConverter}}"" Height=""14"" Width=""14"" HorizontalAlignment=""Right""/>
<TextBlock Text=""{Binding}"" Padding=""5 0""/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>";
//Binding Converter={StaticResource goColorConverter
var sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml));
var pc = new ParserContext();
pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
pc.XmlnsDictionary.Add("local", "clr-namespace:RP_SIL.View;assembly=RP_SIL.View");
var datatemplate = (DataTemplate)XamlReader.Load(sr, pc);
col.CellTemplate = datatemplate;
col.CellEditingTemplate = datatemplate;
非常感谢机器学习花时间解决问题![=16=]
欢迎光临。
在xaml.
中有一个trick加载转换器
这是我演示中的代码,供您参考。
string MyBoolName = "IsEnabled";
string MyTextName = "Title";
string xaml =
@"<DataTemplate
xmlns:local=""clr-namespace:Templating;assembly=Templating""
x:Key=""goDropColumn"">
<ComboBox Name=""combo"" Width=""85"" ItemsSource=""{Binding Path=DataContext.MyThings,
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}""
> <!-- SelectedValue=""{Binding ???}"" -->
<ComboBox.Resources>
<local:BrushColorConverter x:Key=""goColorConverter""></local:BrushColorConverter>
</ComboBox.Resources>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation=""Horizontal"">
<Ellipse Fill=""{Binding ""{Binding "+ MyBoolName + @"}"", Converter={StaticResource goColorConverter}}""
Height=""14"" Width=""14"" />
<TextBlock Text=""{Binding " + MyTextName + @"}"" Padding=""5 0""/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>";
我正在尝试将 DataTemplate 应用于我自动生成的列。我可以用一个简单的模板来做到这一点。但是我想要一个组合框,其中文本绑定到数据网格,椭圆显示基于此值的颜色,通过转换器。
我试过两种方法。首先在代码隐藏中创建模板并通过 xamlReader.
加载它private void LeftPanel_AutoGeneratingColumn(
object sender, DataGridAutoGeneratingColumnEventArgs e)
{
DataGridTemplateColumn col = new DataGridTemplateColumn();
col.Header = e.Column.Header;
string xaml =
"<DataTemplate xmlns:local=\"clr-namespace:myView\">" +
"<DataTemplate.Resources><local:goColorConverter x:Key=\"goColorConverter\" /></DataTemplate.Resources>" +
"<ComboBox " +
"SelectedValue=\"{Binding [" + e.Column.Header +
"], Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}\" Width=\"85\" " +
"ItemsSource=\"{Binding Path=DataContext.goNoGo, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}\">" +
"<ComboBox.ItemTemplate><DataTemplate><StackPanel Orientation=\"Horizontal\">" +
"<Ellipse Fill=\"{Binding Converter={StaticResource goColorConverter}}\" Height=\"14\" Width=\"14\" HorizontalAlignment=\"Right\"/>" +
"<TextBlock Text=\"{Binding}\" Padding=\"5 0\"/>" +
"</StackPanel></DataTemplate></ComboBox.ItemTemplate></ComboBox></DataTemplate>";
var sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml));
var pc = new ParserContext();
pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
var datatemplate = (DataTemplate)XamlReader.Load(sr, pc);
col.CellTemplate = datatemplate;
col.CellEditingTemplate = datatemplate;
e.Column = col;
return;
}
这为我提供了对数据的正确绑定,但我无法使用转换器进行编译。我得到错误无法为我的转换器创建未知类型(存在于我后面的代码中)。我已经阅读了很多关于程序集的帖子,但无法使用它们来帮助我。
我的第二种方法是在 xaml 中定义模板并将其应用到我后面的代码中。这使转换器工作,但我不知道如何定义我选择的值绑定,因为我不知道执行前的列名。我希望使用这个 Josh smith FindName example 但我不知道如何访问内容展示器。
代码:
private void LeftPanel_AutoGeneratingColumn(
object sender, DataGridAutoGeneratingColumnEventArgs e)
{
DataGridTemplateColumn col = new DataGridTemplateColumn();
col.Header = e.Column.Header;
col.CellTemplate = (DataTemplate)Resources["goDropColumn"];
col.CellEditingTemplate = (DataTemplate)Resources["goDropColumn"];
//Do magic here to get combo box and update its binding
e.Column = col;
return;
}
xaml:
<DataTemplate x:Key="goDropColumn">
<ComboBox Name="combo" Width="85" ItemsSource="{Binding Path=DataContext.goNoGo,
RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
SelectedValue="{Binding ???}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Ellipse Fill="{Binding Converter={StaticResource goColorConverter}}"
Height="14" Width="14"/>
<TextBlock Text="{Binding}" Padding="5 0"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
我不喜欢我的方法并且是 WPF 和 c# 的新手所以如果方法完全错误请告诉我。我觉得第二种方法更简洁,如果我可以更改标有 ??? 的绑定,它会起作用。到列名。
Another possibly relevant article 但我没有自定义数据类型,我只想将 selectedValue 绑定到该列。
根据 MachineLearning 的要求更新了代码:
DataGridTemplateColumn col = new DataGridTemplateColumn();
col.Header = e.Column.Header;
string xaml =
@"<DataTemplate x:Key=""goDropColumn""
xmlns:local=""clr-namespace:RP_SIL.View;assembly=RP_SIL.View""
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x = ""http://schemas.microsoft.com/winfx/2006/xaml"">
<ComboBox SelectedValue=""{Binding [" + e.Column.Header + @"], Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}""
Width=""85""
ItemsSource=""{Binding Path=DataContext.goNoGo, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"">
<ComboBox.Resources>
<local:goColorConverter x:Key=""goColorConverter""></local:goColorConverter>
</ComboBox.Resources>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation=""Horizontal"">
<Ellipse Fill=""{Binding Converter={StaticResource goColorConverter}}"" Height=""14"" Width=""14"" HorizontalAlignment=""Right""/>
<TextBlock Text=""{Binding}"" Padding=""5 0""/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>";
//Binding Converter={StaticResource goColorConverter
var sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml));
var pc = new ParserContext();
pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
pc.XmlnsDictionary.Add("local", "clr-namespace:RP_SIL.View;assembly=RP_SIL.View");
var datatemplate = (DataTemplate)XamlReader.Load(sr, pc);
col.CellTemplate = datatemplate;
col.CellEditingTemplate = datatemplate;
非常感谢机器学习花时间解决问题![=16=]
欢迎光临。 在xaml.
中有一个trick加载转换器这是我演示中的代码,供您参考。
string MyBoolName = "IsEnabled";
string MyTextName = "Title";
string xaml =
@"<DataTemplate
xmlns:local=""clr-namespace:Templating;assembly=Templating""
x:Key=""goDropColumn"">
<ComboBox Name=""combo"" Width=""85"" ItemsSource=""{Binding Path=DataContext.MyThings,
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}""
> <!-- SelectedValue=""{Binding ???}"" -->
<ComboBox.Resources>
<local:BrushColorConverter x:Key=""goColorConverter""></local:BrushColorConverter>
</ComboBox.Resources>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation=""Horizontal"">
<Ellipse Fill=""{Binding ""{Binding "+ MyBoolName + @"}"", Converter={StaticResource goColorConverter}}""
Height=""14"" Width=""14"" />
<TextBlock Text=""{Binding " + MyTextName + @"}"" Padding=""5 0""/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>";