Tuesday, January 22, 2013

How To Bind to a Control Reference from a DataTemplate within DataGrid

How To Bind to a Control Reference from a DataTemplate within DataGrid

I am not using MVVM or PRISM based models. I am trying to bind inside a DataGrid DataTemplate to a control that lives on the same level as my DataGrid. When I do this, I am returned null.

2 Questions:

  1. What is LayoutRoot? When I refer to it, which in this case is a Grid, I am returned an object. If I change LayoutRoot to a canvas in my XAML, I am returned null.

  2. How do I bind to a Canvas in my XAML inside the DataTemplate of a DataGrid Column?

I have the following XAML: (Trimmed because of Length)

<Grid x:Name="LayoutRoot" Background="#F7F7F7">     <Border>         <Canvas x:Name="LayoutCanvas">             <!-- A lot of Xaml -->         </Canvas>     </Border> </Grid> 

Within my Canvas, I have the following DataGrid:

<sdk:DataGrid x:Name="dgOrderContents" AutoGenerateColumns="False">     <sdk:DataGrid.Columns>         <sdk:DataGridTemplateColumn Header="Thumb">             <sdk:DataGridTemplateColumn.CellTemplate>                 <DataTemplate>                     <ucp:PhotoComponentForDataGrid Source="{Binding PhotoUrl}" PopupTarget="{Binding ElementName='LayoutCanvas' }" Width="60" />                 </DataTemplate>             </sdk:DataGridTemplateColumn.CellTemplate>         </sdk:DataGridTemplateColumn     </sdk:DataGrid.Columns> </sdk:DataGrid> 

My problem is when I try to bind using 'LayoutCanvas', the value is null which causes an error. If I bind to 'LayoutRoot', it works, except I need the Canvas because a a FloatableWindow control that lives inside my UserControl relies on a Canvas.

Thanks in advance for any help or suggestions.

Answers & Comments...




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog