GridView标题单击

时间:2017-03-29 04:45:24

标签: c# wpf gridview

我想弄清楚如何指向网格视图标题单击我的Viewmodel

 <ListView ItemsSource="{Binding UserProfileData}" GridViewColumnHeader.Click="Handle_Click">
       <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="User ID"/>
                    <GridViewColumn DisplayMemberBinding="{Binding LastUsed}" Header="Last Loaded"/>
                    <GridViewColumn DisplayMemberBinding="{Binding IsLoaded}" Header="Logged In"/>
                </GridView>
            </ListView.View>
        </ListView>

GridViewColumnHeader.Click =&#34; Handle_Click&#34;将它推送到我的MainWindow.xaml.cs,但我希望点击进入我的MainWindow的DataContext

<Window.DataContext>
    <vm:MainWindowViewModel/>
</Window.DataContext>

任何想法?

2 个答案:

答案 0 :(得分:2)

您可以使用样式将Command的{​​{1}}属性绑定到视图模型的GridViewColumnHeader源属性。然后,您可以将标头ICommand作为参数传递给命令:

string

答案 1 :(得分:0)

您需要添加2个程序集的引用:

System.Windows.Interactivity
Microsoft.Expression.Interactions

添加以下命名空间:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"

的Xaml:

<ListView ItemsSource="{Binding UserProfileData}">
        <ListView.View>
            <GridView>
                <GridViewColumn DisplayMemberBinding="{Binding Name}">
                    <GridViewColumnHeader Content="User ID">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <ei:CallMethodAction TargetObject="{Binding}" MethodName="OnClick"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </GridViewColumnHeader>
                </GridViewColumn>

                <GridViewColumn DisplayMemberBinding="{Binding LastUsed}">
                    <GridViewColumnHeader Content="Last Loaded">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <ei:CallMethodAction TargetObject="{Binding}" MethodName="OnClick"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </GridViewColumnHeader>
                </GridViewColumn>

                <GridViewColumn DisplayMemberBinding="{Binding IsLoaded}">
                    <GridViewColumnHeader Content="Logged In">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <ei:CallMethodAction TargetObject="{Binding}" MethodName="OnClick"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </GridViewColumnHeader>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

在视图模型中,输入以下方法:

public void OnClick(object sender, RoutedEventArgs e)
    {

    }