Silverlight DataGrid.Celltemplate 绑定到 ViewModel [英] Silverlight DataGrid.Celltemplate Binding to ViewModel
问题描述
我正在实施 MVVC 模式,但无法从数据网格中的 DataTemplate 中绑定视图模型中的属性.如果我在列中的 DataTemplate 之外有一个文本块,它可以正常工作(因为我直接引用了 UserConrol 的数据上下文,即 VM),但是在 DataTemplate 中它不会返回纯文本属性.然而,它将从迭代的 IEnumerable 项返回一个属性.
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.
<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">
<StackPanel>
<TextBlock Text="{Binding Path=DataContext.testText, ElementName=View}" />
<data:DataGrid Height="280" Width="500" ItemsSource="{Binding TimeSlots}" AutoGenerateColumns="False" >
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="Allocation Area" Binding="{Binding TimeAllocationArea.TimeAllocationName}" Width="200" />
<data:DataGridTextColumn Header="Start" Binding="{Binding StartTime}" Width="80" />
<data:DataGridTextColumn Header="End" Binding="{Binding Path=DataContext.testText, ElementName=View}" Width="80" />
<data:DataGridTemplateColumn Header="Modify" Width="200" >
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=DataContext.testText, ElementName=View}" />
</StackPanel>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>
</data:DataGrid>
</StackPanel>
</UserControl>
我忽略的 DataTemplate 有什么问题吗??请注意,{Binding Path=DataContext.testText, ElementName=View}"适用于除 DataTemplate 中的元素之外的所有元素.(请注意,我知道 DG 外的第一个 Textblock 不需要 ElementName 等,但我只是通过这种方式向自己证明它引用了正确的内容)
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)
推荐答案
我不知道这是否适用于 SL,但您可以查看:
I don't know if this applies to SL, but you can check this out:
Columns 集合只是 Datagrid 中的一个属性;这个集合不在逻辑(或可视化)树中,因此 DataContext 没有被继承,这导致没有任何东西可以绑定."
"The Columns collection is just a property in the Datagrid; this collection is not in the logical (or visual) tree, therefore the DataContext is not being inherited, which leads to there being nothing to bind to."
这篇关于Silverlight DataGrid.Celltemplate 绑定到 ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!