选择/取消复选框与空间/输入列表视图中的GridView控件 - WPF [英] Check/Uncheck checkbox with Space/Enter in gridview in listview - wpf

查看:136
本文介绍了选择/取消复选框与空间/输入列表视图中的GridView控件 - WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GridView控件复选框作为列和的TextBlock 作为另一列。这里是 XAML 代码:

 < ListView.View> 
<&GridView的GT;
< GridView.ColumnHeaderContainerStyle>
<样式和GT;
< setter属性=UIElement.VisibilityVALUE =折叠/>
< /样式和GT;
< /GridView.ColumnHeaderContainerStyle>

< GridViewColumn>
< GridViewColumn.CellTemplate>
<&DataTemplate的GT;
< checkbox标签={结合}=器isChecked{绑定路径= IsFormChecked,模式=双向}IsEnabled ={绑定路径= IsUnRestricted}IsThreeState =FALSEUIElement.KeyUp =CheckBox_KeyUp />
< / DataTemplate中>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>

< GridViewColumn WIDTH =自动>
< GridViewColumn.CellTemplate>
<&DataTemplate的GT;
< TextBlock的宽度=600标签={结合}IsEnabled ={绑定路径= IsUnRestricted}文本={绑定路径=窗体名称}的MouseUp =TextBlock_MouseUp>
< TextBlock.ToolTip>
< TextBlock的文本={绑定路径=窗体名称}/>
< /TextBlock.ToolTip>
< / TextBlock的>
< / DataTemplate中>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>
< / GridView的>
< /ListView.View>

当我访问此 GridView控件与键盘完整的行被选中(复选框以及的TextBlock )。现在,在这一点上,如果我按空格键,没有任何反应。如果我想用键盘来访问复选框,我不得不再次按下,使焦点设置在复选框tab键和我可以检查/使用空格键取消选择。我想要做的就是时,重点是我要检查只有一个空格键的按键/取消选中复选框行。


解决方案

如果你有一个当前选定的项目绑定,你可以处理你的 ListView中 PreviewKeyUp 事件

 < ListView控件PreviewKeyUp =OnGridKeyUp的SelectedItem ={结合MySelectedItem}
的SelectionMode =单的ItemsSource ={结合ItemsList}>

< /&的ListView GT;






,然后在代码隐藏处理这个问题

 私人无效OnGridKeyUp(对象发件人,发送KeyEventArgs E)
{
如果(vm.mySelectedItem = NULL&放大器;!&安培; e.Key ==关键。空间)
{
vm.MySelectedItem.IsChecked = vm.MySelectedItem.IsChecked!;
e.Handled = TRUE; //这是必要的,因为选中该复选框,否则细胞时,将应用此KEYUP,也适用于该复选框
}
}
<默认行为/ pre>

这显然需要你有你的视图模型一个手柄从后面的代码。这可能是一样容易:

  VAR VM =的DataContext作为MyViewModel; 

这是不是最MVVM方式,但嘿...


I have a GridView with CheckBox as a column and TextBlock as another column. Here is the XAML code:

<ListView.View>
    <GridView>
        <GridView.ColumnHeaderContainerStyle>
            <Style>
                <Setter Property="UIElement.Visibility" Value="Collapsed"/>
            </Style>
        </GridView.ColumnHeaderContainerStyle>

        <GridViewColumn>
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox Tag="{Binding}" IsChecked="{Binding Path=IsFormChecked, Mode=TwoWay}" IsEnabled="{Binding Path=IsUnRestricted}" IsThreeState="False" UIElement.KeyUp="CheckBox_KeyUp" />
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>

        <GridViewColumn Width="auto">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Width="600" Tag="{Binding}" IsEnabled="{Binding Path=IsUnRestricted}" Text="{Binding Path=FormName}" MouseUp="TextBlock_MouseUp">
                        <TextBlock.ToolTip>
                            <TextBlock Text="{Binding Path=FormName}"/>
                        </TextBlock.ToolTip>
                    </TextBlock>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
    </GridView>
</ListView.View>

When I access this GridView with the keyboard complete row is selected (CheckBox as well as TextBlock). Now at this point if i press space key, nothing happens. If I want to access the CheckBox with keyboard, I have to press tab key once more so that focus is set on checkbox and the I can check/uncheck it using space bar. What I want to do is when the focus is on the row I want to check/uncheck checkbox with only one key press of space bar.

解决方案

If you have a binding on the currently selected item, you can handle the PreviewKeyUp event on your ListView:

<ListView PreviewKeyUp="OnGridKeyUp" SelectedItem="{Binding MySelectedItem}"
          SelectionMode="Single" ItemsSource="{Binding ItemsList}">
    ...
</ListView>

and then handle this in code-behind:

private void OnGridKeyUp(object sender, KeyEventArgs e)
{
    if(vm.mySelectedItem != null && e.Key == Key.Space)
    {
        vm.MySelectedItem.IsChecked = !vm.MySelectedItem.IsChecked;
        e.Handled = true; //this is necessary because otherwise when the checkbox cell is selected, it will apply this keyup and also apply the default behavior for the checkbox
    }
}

This obviously requires you to have a handle on your viewmodel from your code behind. This could be as easy as:

var vm = DataContext as MyViewModel;

this is not the most MVVM way, but hey...

这篇关于选择/取消复选框与空间/输入列表视图中的GridView控件 - WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆