【发布时间】:2012-11-12 16:25:01
【问题描述】:
我搜索了一下,但我找到的信息不是我需要的。所以我决定问你们所有人——我确定这是一个新手问题,但我真的不明白。 开始吧: 我有一个 DataSource,它是一个分组的可观察集合。目前我有 2 组不同数量的项目。两个组和项属于同一个公共基础:
public DataCommon(String uniqueId, String title, String subtitle, String imagePath, String description)
{
this._uniqueId = uniqueId;
this._title = title;
this._subtitle = subtitle;
this._description = description;
this._imagePath = imagePath;
}
这是模型的构造函数。 在 ViewModel 我填充它。 现在我想将带有命令的 ItemClick 绑定到我的 ViewModel。我喜欢这个(只是一小部分):
<GridView
x:Name="itemGridView"
AutomationProperties.AutomationId="ItemGridView"
AutomationProperties.Name="Grouped Items"
Grid.RowSpan="2"
Padding="116,137,40,46"
ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
ItemTemplate="{StaticResource Standard250x250ItemTemplate}"
SelectionMode="None"
IsSwipeEnabled="false"
IsItemClickEnabled="True"
>
<WinRtBehaviors:Interaction.Behaviors>
<Win8nl_Behavior:EventToCommandBehavior Event="ItemClick" Command="ItemClickCommand" CommandParameter="{Binding UniqueId}"/>
</WinRtBehaviors:Interaction.Behaviors>
但是现在问题来了。在“Binding UniqueId”中,它说 DataContext 是我的 ViewModel,所以我无法将它连接到模型的属性。看着 Page.DataContext 我告诉 XAML 你使用我的 ViewModel 作为 DataContext。我想这是正确的。但是我怎样才能访问模型属性? 我试过这样做(将我的模型定义为 DataModel):
<WinRtBehaviors:Interaction.Behaviors>
<Win8nl_Behavior:EventToCommandBehavior Event="ItemClick" Command="ItemClickCommand" CommandParameter="{Binding DataModel:SampleDataCommon.UniqueId}"/>
</WinRtBehaviors:Interaction.Behaviors>
但正如我之前猜测的那样,它不起作用 - 作为参数,我得到了 null。
我会感谢任何帮助,因为正如我在帖子开头所说的:我真的不明白......
【问题讨论】:
标签: xaml collections windows-runtime eventtocommand