This is a migrated thread and some comments may be shown as answers.

Binding to value outside of DataTemplate

5 Answers 117 Views
Grid for XAML
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Erik
Top achievements
Rank 1
Erik asked on 17 Feb 2014, 01:25 PM
Hi,

I'm having a similar issue to http://www.telerik.com/forums/elementname-binding-inside-of-celltemplate but now I'm speaking Win 8 Store Apps.

I have a RadDataGrid where I want to bind a value from outside of its scope. (Actually this is a much simplified example.) Should this work?


<TextBlock x:Name="MyTextBlock" Text="Test"/>
 
<telerikGrid:RadDataGrid ItemsSource="{Binding MyList}">
    <telerikGrid:RadDataGrid.Columns>
        <telerikGrid:DataGridTemplateColumn SizeMode="Fixed" Header="MyHeader">
            <telerikGrid:DataGridTemplateColumn.CellContentTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Text, ElementName=MyTextBlock}"></TextBlock>
                </DataTemplate>
            </telerikGrid:DataGridTemplateColumn.CellContentTemplate>
        </telerikGrid:DataGridTemplateColumn>
    </telerikGrid:RadDataGrid.Columns>
</telerikGrid:RadDataGrid>


Best regards,
Erik

5 Answers, 1 is accepted

Sort by
0
Rosy Topchiyska
Telerik team
answered on 19 Feb 2014, 03:27 PM
Hi Erik,

Thank you for the question.

It is not possible to use an ElementName Binding in a DataTemplate. A possible solution is to bind both elements to a common property of your view model.

Let us know if you have further questions.

Regards,
Rositsa Topchiyska
Telerik
0
Erik
Top achievements
Rank 1
answered on 20 Feb 2014, 09:31 AM
Hi,

Ok, but I can't seem to get how that works also, see this code. How can I bind the textblock-text to the outer viewmodel, when the datagrid has its own DataContext (MyList, which is also in the ViewModel). Is it by using FindAncestor somehow?

<telerikGrid:RadDataGrid ItemsSource="{Binding MyList}">
    <telerikGrid:RadDataGrid.Columns>
        <telerikGrid:DataGridTemplateColumn SizeMode="Fixed" Header="MyHeader">
            <telerikGrid:DataGridTemplateColumn.CellContentTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding ParentViewModel??.MyText}"></TextBlock>
                </DataTemplate>
            </telerikGrid:DataGridTemplateColumn.CellContentTemplate>
        </telerikGrid:DataGridTemplateColumn>
    </telerikGrid:RadDataGrid.Columns>
</telerikGrid:RadDataGrid>


Best regards,
Erik
0
Rosy Topchiyska
Telerik team
answered on 20 Feb 2014, 06:19 PM
Hello Erik,

Thank you for the question.

I can suggest you two possible solutions:
  1. If your view model is defined as a static resource, there is no problem with binding.
  2. You can introduce a new property in your data class that returns a property from the view model, although the logic in this case is a bit more complicated.
I have attached sample projects to demonstrate both approaches.

I hope this helps. Please, let us know if you have other questions.

Regards,
Rositsa Topchiyska
Telerik
0
Erik
Top achievements
Rank 1
answered on 21 Feb 2014, 01:58 PM
Thanks, having the ViewModel as a property in my data object will work for me!

I find it strange that it is not possible to "walk up the tree", and bind to a parents DataContext.

Cheers,
Erik
0
Rosy Topchiyska
Telerik team
answered on 26 Feb 2014, 09:13 AM
Hello Erik,

I am glad one of the solutions worked for you. The ElementName Binding in the template of the column cell is not available in the RadDataGrid control due to some perfomace optimizations done. In the future we will consider possibilities of how this could be enabled.

Let us know if you have any other questions.

Regards,
Rositsa Topchiyska
Telerik
Tags
Grid for XAML
Asked by
Erik
Top achievements
Rank 1
Answers by
Rosy Topchiyska
Telerik team
Erik
Top achievements
Rank 1
Share this question
or