Note that there are some explanatory texts on larger screens.

plurals
  1. POSilverlight DataGrid.Celltemplate Binding to ViewModel
    primarykey
    data
    text
    <p>I am in the process of implimenting the MVVC pattern and am having trouble binding a property in the viewmodel from within a DataTemplate within a datagrid. If I have a textblock outside the DataTemplate in the column it works fine (since I am directly referencing the datacontext of the UserConrol, i.e. the VM) however from within the DataTemplate it wont return the plain text property. It will however return a property from the iterated IEnumerable item.</p> <pre><code>&lt;UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="Timesheet.Silverlight.Modules.Views.HistoryView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:Commands="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ViewModels="clr-namespace:Timesheet.Silverlight.Modules.ViewModels" x:Name="View"&gt; &lt;StackPanel&gt; &lt;TextBlock Text="{Binding Path=DataContext.testText, ElementName=View}" /&gt; &lt;data:DataGrid Height="280" Width="500" ItemsSource="{Binding TimeSlots}" AutoGenerateColumns="False" &gt; &lt;data:DataGrid.Columns&gt; &lt;data:DataGridTextColumn Header="Allocation Area" Binding="{Binding TimeAllocationArea.TimeAllocationName}" Width="200" /&gt; &lt;data:DataGridTextColumn Header="Start" Binding="{Binding StartTime}" Width="80" /&gt; &lt;data:DataGridTextColumn Header="End" Binding="{Binding Path=DataContext.testText, ElementName=View}" Width="80" /&gt; &lt;data:DataGridTemplateColumn Header="Modify" Width="200" &gt; &lt;data:DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="{Binding Path=DataContext.testText, ElementName=View}" /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/data:DataGridTemplateColumn.CellTemplate&gt; &lt;/data:DataGridTemplateColumn&gt; &lt;/data:DataGrid.Columns&gt; &lt;/data:DataGrid&gt; &lt;/StackPanel&gt; &lt;/UserControl&gt; </code></pre> <p>Is there some sort of problem with the DataTemplate that im ignoring?? Note the "{Binding Path=DataContext.testText, ElementName=View}" works for all elements except the one in the DataTemplate. (Note I know that the 1st Textblock outside the DG doesnt need the ElementName etc but i have just done it this way to prove to myself that its referencing the right thing)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload