c# - UWP 数据绑定(bind) : How to set button command to parent DataContext inside DataTemplate

标签 c# xaml data-binding uwp datacontext

需求的简短说明:我需要使用 ViewModel 的 DataContext 中的方法触发 DataTemplate 中按钮的命令。

问题的简短说明:模板化按钮命令似乎只能绑定(bind)到项目本身的数据上下文。 WPF 和 Windows 8.1 应用程序用于在可视化树中向上移动的语法似乎不起作用,包括 ElementName 和 Ancestor 绑定(bind)。我非常不希望我的按钮命令位于模型中。

旁注:这是使用 MVVM 设计方法构建的。

下面的代码生成了 VIEW 上的项目列表。该列表是每个列表项的一个按钮。

            <ItemsControl x:Name="listView" Tag="listOfStories" Grid.Row="0" Grid.Column="1"
            ItemsSource="{x:Bind ViewModel.ListOfStories}"
            ItemTemplate="{StaticResource storyTemplate}"
            Background="Transparent"
            IsRightTapEnabled="False"
            IsHoldingEnabled="False"
            IsDoubleTapEnabled="False"
                 />

在同一个 View 的页面资源中,我创建了一个 DataTemplate,其中包含有问题的按钮。我继续并删除了按钮内的大部分格式,例如文本,以使代码在这一侧更易于阅读。与按钮相关的一切都正常,除了列出的问题,即命令的绑定(bind)。

<Page.Resources>
        <DataTemplate x:Name="storyTemplate" x:DataType="m:Story">
            <Button
                Margin="0,6,0,0"
                Width="{Binding ColumnDefinitions[1].ActualWidth, ElementName=storyGrid, Mode=OneWay}"
                HorizontalContentAlignment="Stretch"
                CommandParameter="{Binding DataContext, ElementName=Page}"
                Command="{Binding Source={StaticResource Locator}}">

                <StackPanel HorizontalAlignment="Stretch" >
                    <TextBlock Text="{x:Bind StoryTitle, Mode=OneWay}"
                        FontSize="30"
                        TextTrimming="WordEllipsis"
                        TextAlignment="Left"/>
                </StackPanel>
            </Button>
        </DataTemplate>
    </Page.Resources>

因为这是一个 DataTemplate,所以 DataContext 已设置为构成列表 (MODEL) 的各个项目。我需要做的是选择列表本身的 DataContext (VIEWMODEL),这样我就可以访问导航命令。

如果您对VIEW页面的代码隐藏感兴趣,请看下面。

    public sealed partial class ChooseStoryToPlay_View : Page
    {
    public ChooseStoryToPlay_View()
    {
        this.InitializeComponent();
        this.DataContextChanged += (s, e) => { ViewModel = DataContext as ChooseStoryToPlay_ViewModel; };
    }
    public ChooseStoryToPlay_ViewModel ViewModel { get; set; }
}

我已经尝试通过 ElementName 设置它,以及许多其他尝试,但都失败了。当输入 ElementName 时,Intellisense 将“storyTemplate”检测为一个选项,这是该问题第一个代码块中显示的 DataTemplate 的名称。

我不认为我的问题是独一无二的,但是我很难找到 UWP 的解决方案。请允许我提前道歉,因为这是一个简单的问题,但我花了将近两天的时间研究答案,似乎没有一个适用于 UWP。

谢谢大家!

最佳答案

您使用的是什么 MVVM 工具包(如果有)?在 MVVM Light 中,您可以通过为 View 设置 DataContext 的相同方式从 DataTemplate 获取 ViewModel:

<DataTemplate x:Key="SomeTemplate">
    <Button Command="{Binding Main.MyCommand, Source={StaticResource ViewModelLocator}}"/>
</DataTemplate>

关于c# - UWP 数据绑定(bind) : How to set button command to parent DataContext inside DataTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34968211/

相关文章:

c# - 在 C# 中使用通配符搜索进行字符串比较

wpf - 使用 WPF 进行数据绑定(bind)

c# - 我可以以某种方式暂时禁用 WPF 数据绑定(bind)更改吗?

javascript - 从代码隐藏向 jQuery 函数发送动态参数

c# - 从 MySql 返回最后插入的 id

c# - 改变可写位图的大小

c# - 将 Grid.RowDefinition 高度绑定(bind)到 Xamarin 中的网格宽度

java - 使用 Comboviewer 进行 JFace 数据绑定(bind)

c# - 如何在使用自定义窗口镶边时向 WPF 标题栏添加按钮?

c# - 将样式库加载到 WPF 中