简体   繁体   English

以编程方式绑定DataGridTemplateColumn

[英]Programmatically bind DataGridTemplateColumn

Here is my code: 这是我的代码:

foreach (var columnData in lookup.DataProvider.Metadata)
    {
        DataGridColumn column = new DataGridTextColumn { Binding = new Binding(columnData.FieldName) };

        if (columnData.DataType == typeof(bool))
        {
            column = new DataGridCheckBoxColumn { Binding = new Binding(columnData.FieldName) };
        }

        if (columnData.DataType == typeof(DateTime))
        {
            column = new DataGridTemplateColumn();
            //... ????
        }

        column.Header = columnData.Caption;

        DataDataGrid.Columns.Add(column);
    }

Basically, I'm creating columns and bindings in code because columns not known at design-time. 基本上,我在代码中创建列和绑定,因为列在设计时是未知的。

Now I need to add templated column and not sure how to write it in C#. 现在我需要添加模板化列,而不确定如何在C#中编写它。 Here is example of XAML of column I need to add: 以下是我需要添加的列的XAML示例:

<sdk:DataGridTemplateColumn Header="Received" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" SortMemberPath="SomeTime">
    <sdk:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <MyControls:MyDateTimeLabel DisplayUtcDate="{Binding SomeTime}" />
        </DataTemplate>
    </sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>

EDIT 编辑

In case someone interested. 万一有人感兴趣。 I used solution from here: http://www.pettijohn.com/2011/01/silverlight-datagrid-with-dynamic.html 我在这里使用了解决方案: http//www.pettijohn.com/2011/01/silverlight-datagrid-with-dynamic.html

I took version with XAML loader. 我用XAML加载器购买了版本。 It definitely smells since I got my namespaces etc hardcoded into strings. 它确实闻起来,因为我将我的命名空间等硬编码到字符串中。

So, I started to explore second choice. 所以,我开始探索第二选择。 Here is how my dynamic column looks now: 以下是我的动态列现在的样子:

column = new DataGridTemplateColumn
            {
                CanUserSort = true,
                SortMemberPath = columnData.FieldName,
                CellTemplate = (DataTemplate)this.Resources["DateTimeColumnDataTemplate"]
            };

I'm loading DateTemplate from resources. 我正在从资源加载DateTemplate This was cool, but how do I do binding? 这很酷,但我该怎么做绑定? Suggestion here was to get to my DateTimeLabel and set binding. 这里的建议是到达我的DateTimeLabel并设置绑定。 But that didn't work(see article on why). 但那不起作用(见有关原因的文章)。 So, I wrote this code and all is well: 所以,我写了这段代码,一切都很好:

private void OnLoadingRow(object sender, DataGridRowEventArgs e)
    {
        foreach (DataGridColumn t in this.DataDataGrid.Columns)
        {
            if (t is DataGridTemplateColumn)
            {
                var label = t.GetCellContent(e.Row) as DitatDateTimeLabel;
                label.SetBinding(DitatDateTimeLabel.DisplayUtcDateProperty, new Binding(t.SortMemberPath));
            }
        }
    }

You could put your DataTemplate inside Page / UserControl resources, retrieve it in code and apply to your column's CellTemplate . 您可以将DataTemplate放在Page / UserControl资源中,在代码中检索它并应用于列的CellTemplate It would look sth like this: 看起来像这样:

column.CellTemplate = (DataTemplate)this.Resources["DateTimeFieldTemplate"];

The binding should work as it is in your DataTemplate XAML right now because on the DataGrid row level your DataContext will be set to the item itself. 绑定应该像在DataTemplate XAML中一样工作,因为在DataGrid行级别, DataContext将被设置为项目本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WPF - 如何绑定DataGridTemplateColumn - WPF - How to bind a DataGridTemplateColumn 绑定到包含ComboBox的DataGridTemplateColumn - Bind to DataGridTemplateColumn containing ComboBox 无法在 DataGridTemplateColumn 中绑定 slider 值 - Cannot bind slider value in DataGridTemplateColumn x:Bind DataTemplate in DataGridTemplateColumn.CellTemplate 不显示内容 - x:Bind DataTemplate in DataGridTemplateColumn.CellTemplate is not displaying content DataGridTemplateColumn中的ComboBox.SelectedItem不绑定到其自己的DataGridRow - ComboBox.SelectedItem in DataGridTemplateColumn doesn't bind to its own DataGridRow 如何将 DataGridTemplateColumn 的 Visibility 绑定到 textBlock 的 Visibility - How to bind DataGridTemplateColumn's Visibility to textBlock's Visibility 如何使用WPF将相关对象属性绑定到DataGridTemplateColumn中的TextBlock - How to bind a related object property to a TextBlock in DataGridTemplateColumn with WPF 如何绑定列表<string>并将其集合显示到 DataGridTemplateColumn</string> - How to bind List<string> and display its collection into a DataGridTemplateColumn 如何在 DataGridTemplateColumn 内的控件上双向绑定属性? - How to bind a property both ways on a control inside a DataGridTemplateColumn? 如何在WPF中使用DataGridTemplateColumn绑定用户控件失败 - How to bind a user control using as a DataGridTemplateColumn failed in WPF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM