1

I have a User table and I have a foreign key of this table from a Book table. the FK is ProxyResponsibleUser_ID. when I use DataGridTextColumn in my DataGrid anything is OK but now I want to use DataGridTemplateColumn to display the FullName column from User table for corresponding user with ProxyResponsibleUser_ID. I get an error since DataGridTemplateColumn does not have a Binding property.

So, by which property of DataGridTemplateColumn will I bind the ProxyResponsibleUser_ID? Thanks in advance.

<DataGridTextColumn x:Name="securityConfigurationNameColumn" Binding="{Binding Path=SecurityConfigurationName}" Header="Security Configuration Name" Width="*" />
<DataGridTemplateColumn x:Name="proxyResponsibleUser_IDColumn"   Binding="{Binding Path=ProxyResponsibleUser_ID}" Header="Proxy Responsible User ID" Width="*" >
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate DataType="{x:Type domain:User}">
                        <TextBlock Text="{Binding FullName}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

2 Answers 2

4

I have been struggling with this question for quite a while, and after many fruitless searches, I came up with an acceptable substitute.

To anyone else searching for this: Try creating a custom implementation of DataGridBoundColumn. You will only need to override two methods: GenerateElement and GenerateEditingElement.

Example:

protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
{
    Label element = new Label();
    element.SetBinding(Label.ContentProperty, Binding);

    return element;
}

Usage:

<DataGrid>
    <DataGrid.Columns>
        <DataGridCustomColumn Binding="{Binding SomeProperty}" />
        <DataGridCustomColumn Binding="{Binding OtherProperty}" />
    </DataGrid.Columns>
</DataGrid>

Though I originally intended to use templates, I have opted to create elements in the code-behind as my needs are rather simple; however, I see no reason why this could not be adapted to work with DataTemplates given the proper DependencyProperty registrations.

Sign up to request clarification or add additional context in comments.

Comments

0

I don't see exactly what you want to do, but the Binding has to be done at the TextBlock exactly as you already did. Therefore the Binding in the DataGridTemplateColumn-Tag is needless. There is no need for the column to know the id of the record.

If you want to have the id available to the TextBox-control (commonly not needed because you have access to this property directly through the DataContext), you can do this for example by binding the Tag-property.

<TextBlock Text="{Binding FullName}" Tag="{Binding ProxyResponsibleUser_ID}"/> 

By the way, you declared a TextBlock in the EditTemplate. Maybe you want a TextBox. Or do you want to allow the user to change the Id? Make a comment if this is the case.

Hope this helps.

3 Comments

Normally I was using DataGridTextColumn to display the FullName via the User navigation by Entity FrameWork. When I change a combobox's selected item, I want the datagrid value to be updated but when I do so, there had been update problems. bur when I use directly the proxyResponsibleUser_ID, it is automatically updated but in data grid Id values are displayed instead of Fullname. So I tried such a code snippet. Thanks for your help but it didn't work. Can you suggest a solution for such a problem? Thank you again..
No I dont want anybody to edit the column, I used it by mistake :p now I used celltemplate
Sounds to me as an EF-problem (synchronizing between key and navigation-property). But I'm sorry, I have no knowledge about EF and therefore can not help you in this case. However for the question you wrote, DataGridTemplateColumn has no need to know the Binding. All binding-specific declaration is done in the DataTemplate/s. I would try to ask a more EF-specific question to get a more valuable answer than mine (my answer is only WPF-specific).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.