簡體   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