We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Assign DataContext to cells of a column

Thread ID:

Created:

Updated:

Platform:

Replies:

116140 Apr 10,2014 09:00 PM UTC Apr 17,2014 02:43 PM UTC WPF 5
loading
Tags: GridDataControl
Michael Cheng
Asked On April 10, 2014 09:00 PM UTC

I'm having a real hard time attempting to set DataContext for cells of a column so I can bind elements in the cell item template to the DataContext (that is an object). It seems that the only way is to use UnboundColumn and the only way to get the object is to get the object that is bound to the row through: Record.Data.MyObject.MyProperty.

Instead, I would like to be able to say (simplified xaml):
<UnboundColumn binding="{binding MyObject}">
    <DataTemplate>
        <TextBlock Text="{binding MyProperty}"/>
    </DataTemplate>
</UnboundColumn>

That is what I would expect from how data binding works. Other grid controls seem to work that way. Am I missing something or does Syncfusion grid not able to do that?

Jayapradha S [Syncfusion]
Replied On April 14, 2014 05:19 PM UTC

Hi Michael,

 

Thanks for contacting Syncfusion support.

 

You can achieve your requirement by setting CellType as DataBoundTemplate and set datatemplate as specified in the way.

Please refer the below code snippet,

 

Code Snippet:

<syncfusion:GridDataUnboundVisibleColumn>

<syncfusion:GridDataUnboundVisibleColumn.ColumnStyle>

<syncfusion:GridDataColumnStyle CellType="DataBoundTemplate">

<syncfusion:GridDataColumnStyle.CellItemTemplate>

<DataTemplate>

<TextBlock Text="{Binding Record.Data.Suppliers.CompanyName}"/>

</DataTemplate>

</syncfusion:GridDataColumnStyle.CellItemTemplate>

</syncfusion:GridDataColumnStyle>

</syncfusion:GridDataUnboundVisibleColumn.ColumnStyle>                </syncfusion:GridDataUnboundVisibleColumn>

 

Please let us know if you have any queries.

 

Regards,

Jayapradha

 


Michael Cheng
Replied On April 15, 2014 08:27 PM UTC

So that I confirms there's no way to specify DataContext at the column level. 

So I did what you suggested and documented by Syncfusion for specifying DataTemplate for Unbound Columns. Now I'm also using Style triggers in my DataTemplate and it is causing errors in Debug output:
 
My DataTemplate looks like this:
 
<DataTemplate>
                  <TextBlock Padding="3,5,8,5" DataContext="{Binding Record.Data.TestObjs[Period1]}" Text="{Binding Prop}" FontSize="14">
                    <TextBlock.Style>
                      <Style TargetType="TextBlock">
                        <Style.Triggers>
                          <DataTrigger Binding="{Binding IsTrue}" Value="True">
                            <Setter Property="Background" Value="Red"></Setter>
                          </DataTrigger>
                        </Style.Triggers>
                      </Style>
                    </TextBlock.Style>
                  </TextBlock>
Errors in Debug output:

System.Windows.Data Error: 40 : BindingExpression path error: 'IsTrue' property not found on 'object' ''GridDataCellBoundWrapper' (HashCode=12369902)'. BindingExpression:Path=IsTrue; DataItem='GridDataCellBoundWrapper' (HashCode=12369902); target element is 'TextBlock' (Name=''); target property is 'NoTarget' (type 'Object')

The triggers in DataTemplates still do the jobs right and setting properties correctly, but I would prefer not to see these messages. What should I do??
 
Thank you.

Michael Cheng
Replied On April 15, 2014 08:32 PM UTC

Oh, and I don't want to have to keep repeating "Record.Data" in every binding when I can set the DataContext.

Jayapradha S [Syncfusion]
Replied On April 17, 2014 01:32 PM UTC

Hi Michael,

 

We analyzed your previous code snippet and you need to bind IsTrue property as shown in the below code snippet. Since GridDataCellBoundWrapper bind the property in two ways.

One is Record.Data and another one is directly binds the CellBoundValue.

 

 

<DataTrigger Binding="{Binding Record.Data. IsTrue}" Value="True">
  <Setter Property="Background" Value="Red"></Setter>
</DataTrigger>

 

 

You need not bind the property for each time, instead, you can set the celltype as DataBoundTemplate and bind the cellboundvalue.  CellBoundValue loads the value based on the specified MappingName

 

Please refer the below code snippet,

 

Code snippet:

<syncfusion:GridDataColumnStyle CellType="DataBoundTemplate">

<syncfusion:GridDataColumnStyle.CellItemTemplate>

<DataTemplate>

<TextBlock Text="{Binding CellBoundValue}" VerticalAlignment="Center"/>

</DataTemplate>

</syncfusion:GridDataColumnStyle.CellItemTemplate>

</syncfusion:GridDataColumnStyle>

 

Kindly let us know if this solution helps you.

 

Regards,

Jayapradha

 


Michael Cheng
Replied On April 17, 2014 02:43 PM UTC

Maybe I'm still missing something but that did not work. However, "Record.Data" did. Here's what I have now:

The grid is bound to a list of objects. Each object has a property that is "IDictionary<string, Item> ItemsDictionary {get;set;}". And I want to bind a column to one Item object in the dictionary. Thus "ItemsDictionary[Item1]". "Item1" being the key. And then I want to display the "Name" property of an Item Object.

Looks like this:
public class ViewModel
{
    public ObservableCollection<Thing> Things { get; set; }
}

public class Thing
{
    public IDictionary<string, Item> ItemsDictionary { get; set; }
}

public class Item
{
    public string Name { get; set; }
}

So ultimately, I would like to be able to do the following xaml leveraging basic databinding without generating debug error statements. This works with "Record.Data" but it generates error debug statements (BindingExpresssionErrors).. And using CellBoundValue doesn't work at all.

<syncfusion:GridDataUnboundVisibleColumn HeaderText="Random String" Width="*" MappingName="ItemsDictionary[Item1]" Binding="{Binding ItemsDictionary[Item1]}" AllowDrag="True">
              <syncfusion:GridDataUnboundVisibleColumn.ColumnStyle>
                <syncfusion:GridDataColumnStyle CellTypeEnum="DataBoundTemplate">
                  <syncfusion:GridDataColumnStyle.CellItemTemplate>
                    <DataTemplate>
                      <TextBlock Padding="3,5,8,5" DataContext={Binding CellBoundValue} Text="{Binding Name}" FontSize="14">
                        <TextBlock.Style>
                          <Style TargetType="TextBlock">
                            <Style.Triggers>
                              <DataTrigger Binding="{Binding IsTrue}" Value="True">
                                <Setter Property="Background" Value="Red"></Setter>
                              </DataTrigger>
                            </Style.Triggers>
                          </Style>
                        </TextBlock.Style>
                      </TextBlock>
                    </DataTemplate>
                  </syncfusion:GridDataColumnStyle.CellItemTemplate>
                </syncfusion:GridDataColumnStyle>
              </syncfusion:GridDataUnboundVisibleColumn.ColumnStyle>
            </syncfusion:GridDataUnboundVisibleColumn>

Also, from what you said, sounds like "Record.Data" and "CellBoundValue" are equivalent? But "Record.Data" is the object the whole row is bound to, but your example seems to show "CellBoundValue" is whatever property specified in binding that is a part of the row data object but not the object itself. I'm unclear about this, either.

Thank you for working with me.

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;