繁体   English   中英

WPF Listview:检测何时选择了listviewitem,然后进行检查

[英]WPF Listview: Detect when listviewitem is selected and then check it

我有以下列表视图:

<ListView Margin="10" Name="lvUsers" AlternationCount="2" SelectionMode="Extended">

<ListView.View>
    <GridView>
        <!-- Checkbox header -->
            <GridViewColumn>

                <GridViewColumn.Header>
                    <CheckBox Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
                </GridViewColumn.Header>

                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox IsChecked="{Binding IsChecked}" />
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>

            <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
        <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
        <GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />
    </GridView>
</ListView.View>

    <!-- SELECTED ITEM EVENT -->
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_MouseLeftButtonDown" />
        </Style>
    </ListView.ItemContainerStyle>

</ListView>

和该事件的代码隐藏:

    private void ListViewItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var item = sender as ListViewItem;
        if (item != null && item.IsSelected)
        {
            //Do your stuff
        }

    }

这是数据模型:

public class User : INotifyPropertyChanged
{
    private bool isChecked = false;
    private string name = string.Empty;
    private int age = 0;
    private string mail = string.Empty;

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public bool IsChecked {
        get
        {
            return this.isChecked;
        }

        set
        {
            if (value != this.isChecked)
            {
                this.isChecked = value;
                NotifyPropertyChanged("IsSelected");
            }
        }
    }

    public string Name
    {
        get
        {
            return this.name;
        }

        set
        {
            if (value != this.name)
            {
                this.name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }

    public int Age {
        get
        {
            return this.age;
        }

        set
        {
            if (value != this.age)
            {
                this.age = value;
                NotifyPropertyChanged("Age");
            }
        }
    }

    public string Mail {
        get
        {
            return this.mail;
        }

        set
        {
            if (value != this.mail)
            {
                this.mail = value;
                NotifyPropertyChanged("Mail");
            }
        }
    }
}

我在listview标头有一个复选框,每个listview项都有一个复选框。

我试图检测何时选择了listviewitem,然后一旦选择,我想将其标记为已选中。 触发项目时,Listviewitem事件PreviewMouseLeftButtonDown不起作用。IsSelected为false,因为它是在按下鼠标左键之前进行的预览。 没有MouseClick事件,只有MouseDoubleClick。

另外,一旦单击listviewitem,我想将选中的项目标记为选中状态(选中复选框)。

我怎样才能做到这一点?

在ListViewItem样式中绑定IsSelected属性:

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="IsSelected" Value="{Binding IsChecked}"/>
    </Style>
</ListView.ItemContainerStyle>

请注意,为避免属性名称出现印刷错误,可以使用CallerMemberName属性,该属性使编译器生成正确的属性名称:

using System.Runtime.CompilerServices;
...

private void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public bool IsChecked
{
    get { return isChecked; }
    set
    {
        isChecked = value;
        NotifyPropertyChanged();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM