将通用ObservableCollection绑定到Listview的ItemsSource,会杀死XAML Designer和Visual Studio

时间:2017-04-14 13:58:18

标签: c# xaml mvvm

当我的应用启动时,我正在设置我的窗口的DataContext:

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

然后我将ObservableCollection绑定到ListView,稍后使用:

<ListView Margin="10"
          MinHeight="0"
          BorderThickness="0"
          ItemsSource="{Binding Clients}">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Client ID" Width="85" DisplayMemberBinding="{Binding ClientId}"/>
                        <GridViewColumn Header="Client Name" Width="220" DisplayMemberBinding="{Binding ClientName}"/>
                        <GridViewColumn Header="Branch" Width="60" DisplayMemberBinding="{Binding Branch}" />
                        <GridViewColumn Header="Client Number" Width="125" DisplayMemberBinding="{Binding ClientNumber}"/>
                        <GridViewColumn Header="Fed ID" Width="75" DisplayMemberBinding="{Binding FedId}"/>
                        <GridViewColumn Header="Account Manager" Width="155" DisplayMemberBinding="{Binding AccountManager}"/>
                        <GridViewColumn Header="Region" Width="61" DisplayMemberBinding="{Binding Region}"/>
                    </GridView>
                </ListView.View>
</ListView>

启动时查询该客户端列表,并在默认视图模型的构造函数中进行设置。问题是应用程序现在甚至无法启动 - 但客户端列表在XAML Designer中绑定并可见!它一般会使Visual Studio陷入停顿。这里的问题是什么?

编辑:这里是绑定数据的方式(GetClients只运行一个查询,将结果填充到ObservableCollection中) -

public class DefaultViewModel : INotifyPropertyChanged
{
    public DefaultViewModel()
    {
        _clients = DataSource.GetClients();
    }

    private ObservableCollection<Client> _clients;
    public ObservableCollection<Client> Clients
    {
        get { return _clients; }
        set { Set(ref _clients, value); }
    }


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

    public bool Set<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
    {
        if (Equals(storage, value))
            return false;
        storage = value;
        RaisePropertyChanged(propertyName);
        return true;
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

以下是GetClients功能:

        public static ObservableCollection<Client> GetClients()
    {
        var clients = new ObservableCollection<Client>();
        var query = File.ReadAllText(@"file_path");

        using (var cn = new OleDbConnection(pia_connection_string))
        {
            var cmd = new OleDbCommand(query, cn);
            cn.Open();

            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    var client = new Client
                    {
                        ClientId = int.Parse(reader[0].ToString())
                    };
                    clients.Add(client);
                }
            }
        }

        return clients;
    }

0 个答案:

没有答案