我想将DataTemplate应用于我自动生成的列。我可以用一个简单的模板来做到这一点。不过,我想要一个组合框与绑定到数据网格的文本和一个椭圆形通过转换器显示基于此值的颜色。Datagrid自动生成的列自定义模板绑定
我试过两种方法。首先在代码隐藏中创建模板并通过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绑定到列。
,通过机器学习要求更新的代码:
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;
海量由于机器学习他的时间和解决的问题!
尝试'的xmlns:本地= “” CLR的命名空间:RP_SIL.View;装配= RP_SIL “”' – 2016-08-05 14:49:45
这** **工作的xmlns':本地= “” CLR的命名空间:RP_SIL.View; assembly = RP-SIL“”'您可以在项目属性中找到正确的程序集名称:在这种情况下,它具有一个点而不是下划线。现在请尝试接受我的回答,谢谢 – 2016-08-05 15:57:27