焦点丢失时WPF ListView选择

时间:2015-12-07 22:22:11

标签: wpf listview mvvm command

我目前正在学习并开发简单的WPF(MVVM模式)应用程序,该应用程序允许从一个列表视图(可用项目)中选择项目到另一个(订单),并创建订单类实例一次'购买'按钮正在按下。

我的问题是,一旦我点击第一个listview - 项目被选中,一旦焦点丢失,我就无法取消选择它。

我已经在MVVM中学到了很多关于事件和命令的知识,但事情真的很混乱。您能否以简单的方式指导我如何在列表视图中丢失焦点后刷新' /取消选择所有项目?

谢谢。

1 个答案:

答案 0 :(得分:0)

这是使用绑定的快速而肮脏的示例。您可能需要根据需要调整一些:

XAML

<Grid>

    <ListBox Name="customerList1"
                ItemsSource="{Binding List1Customers}"
                SelectedItem="{Binding List1SelectedCustomer, Mode=TwoWay}"/>

    <ListBox Name="customerList2"
                ItemsSource="{Binding List2Customers}"
                SelectedItem="{Binding List2SelectedCustomer, Mode=TwoWay}"/>

</Grid>

ViewModel

public class Customer
{
    public int id { get; set; }
}

public class MyViewModel
{
    // This is the collection the first list gets its data from
    private ObservableCollection<Customer> list1Customers;
    public ObservableCollection<Customer> List1Customers
    {
        get { return list1Customers; }
        set 
        {
            if (list1Customers != value)
            {
                list1Customers = value;
            }
        }
    }

    // This is the customer selected in the first list
    private Customer list1SelectedCustomer;
    public Customer List1SelectedCustomer
    {
        get { return list1SelectedCustomer; }
        set
        {
            if (list1SelectedCustomer != value)
            {
                list1SelectedCustomer = value;
            }
        }
    }



    // This is the collection the second list gets its data from
    private ObservableCollection<Customer> list2Customers;
    public ObservableCollection<Customer> List2Customers
    {
        get { return list2Customers; }
        set
        {
            if (list2Customers != value)
            {
                list2Customers = value;
            }
        }
    }

    // This is the customer selected in the second list
    private Customer list2SelectedCustomer;
    public Customer List2SelectedCustomer
    {
        get { return list2SelectedCustomer; }
        set
        {
            if (list2SelectedCustomer != value)
            {
                list2SelectedCustomer = value;
            }
        }
    }


    public void MoveCustomer(int id)
    {
        // Find the customer from list 1
        var customerToMove = List1Customers.Where(x => x.id == id).FirstOrDefault();

        // If it was found...
        if (customerToMove != null)
        {
            // Make the first customer null, which un selects it in the list
            List1SelectedCustomer = null;

            // Remove the customer from list 1, or comment out if you dont want it removed
            List1Customers.Remove(customerToMove);

            // Add the customer to list 2
            List2Customers.Add(customerToMove);

            // Make the newly addec customer in list 2 selected
            List2SelectedCustomer = List2Customers.Where(x => x.id == id).FirstOrDefault();
        }
    }
}