Datagrid自动生成列自定义模板绑定

时间:2016-08-03 10:37:43

标签: c# wpf wpfdatagrid

我正在尝试将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;

非常感谢机器学习他的时间和解决问题!

1 个答案:

答案 0 :(得分:0)

欢迎。 在{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>";