【问题标题】:Windows store app, How to get the header value of gridview?Windows商店应用程序,如何获取gridview的标题值?
【发布时间】:2013-07-21 22:37:24
【问题描述】:

这是.xaml中gridview的groupstyle。

        <GridView.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <Button x:Name="HeaderBtn" Click="HeaderButton_Click">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock x:Name="Title" Text="{Binding Key}" />
                                <TextBlock Text="{StaticResource ChevronGlyph}" />
                            </StackPanel>
                        </Button>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
             </GourpStyle>
        </GridView.GroupStyle>

绑定值key是list的key。

这是按钮操作。

    private void HeaderButton_Click(object sender, RoutedEventArgs e)
    {

    }

当我点击按钮时,如何获取 x:Name 为“Title”的文本块的文本值?

【问题讨论】:

    标签: c# xaml binding windows-store-apps datatemplate


    【解决方案1】:

    2 个选项:

    MVVM 方法是使用 ICommand 代替事件并将值作为命令参数传递。

    <Button x:Name="HeaderBtn" Command="{Binding HeaderCommand}" CommandParameter="{Binding Title}">
    

    在事件处理程序中,应该可以检索到按钮的数据上下文,以及title的值

    private void HeaderButton_Click(object sender, RoutedEventArgs e)
    {
    var button = (Button)sender;
    var dataContext = (HeaderClass)button.DataContext;
    var title = dataContext.Title; //here is your value
    }
    

    【讨论】:

    • 谢谢,我已经解决了。但是您的意思是建议使用 ICommand 吗?为什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多