C# WPF Listview Checkboxcolumn绑定
将Checkboxcolumn绑定到Listview时遇到问题。我做错了什么?XAML代码:C# WPF Listview Checkboxcolumn绑定,c#,wpf,listview,checkbox,C#,Wpf,Listview,Checkbox,将Checkboxcolumn绑定到Listview时遇到问题。我做错了什么?XAML代码: <ListView Name="listViewClients" Grid.Row="1" Margin="0,5,0,10" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> <ListView.Resources> <Style TargetType="{x:Type GridViewC
<ListView Name="listViewClients" Grid.Row="1" Margin="0,5,0,10" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.Resources>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Left" />
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="Auswahl" Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding isChecked}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Anrede" Width="50" DisplayMemberBinding="{Binding salutation}" />
<GridViewColumn Header="Titel" Width="50" DisplayMemberBinding="{Binding title}" />
<GridViewColumn Header="Vorname" Width="100" DisplayMemberBinding="{Binding first_name}" />
<GridViewColumn Header="Nachname" Width="110" DisplayMemberBinding="{Binding last_name}" />
<GridViewColumn Header="UnitNr" Width="105" DisplayMemberBinding="{Binding commercialunitnumber}" />
</GridView>
</ListView.View>
</ListView>
然后我将填充列表视图的C代码更改为:
listViewClients.Items.Clear();
for (int a = 0; a < clients.Count; a++)
{
listViewClients.Items.Add(new Clients
{
isChecked = false,
salutation = clients[a].salutation,
title = clients[a].title,
first_name = clients[a].first_name,
last_name = clients[a].last_name,
commercialunitnumber = clients[a].commercialunitnumber,
});
}
listViewClients.Items.Clear();
对于(int a=0;a
我唯一更改的是listViewClients.Items.Add(新建客户端{…}) 请使用可写属性创建适当的视图模型类,因为如前所述“匿名类型将创建只读属性” 我在这里添加了一个客户端视图模型实现的示例
inotifypropertychanged
(我没有添加所有属性)
然后,您只需在创建列表时使用它:
listViewClients.Items.Clear();
for (int a = 0; a < clients.Count; a++)
{
listViewClients.Items.Add(new Clients
{
isChecked = false,
salutation = clients[a].salutation,
title = clients[a].title,
first_name = clients[a].first_name,
last_name = clients[a].last_name,
commercialunitnumber = clients[a].commercialunitnumber,
});
}
listViewClients.Items.Clear();
对于(int a=0;a
匿名类型将创建只读属性,默认情况下,复选框将双向绑定,这是不允许的。创建具有可写属性的适当视图模型类感谢您的评论,这很有帮助:-)感谢您的回答:-)我已经找到了解决方案(请参阅我文章的编辑部分),但我将看看如何实现InotifyPropTyChanged。。。
listViewClients.Items.Clear();
for (int a = 0; a < clients.Count; a++)
{
listViewClients.Items.Add(new Clients
{
isChecked = false,
salutation = clients[a].salutation,
title = clients[a].title,
first_name = clients[a].first_name,
last_name = clients[a].last_name,
commercialunitnumber = clients[a].commercialunitnumber,
});
}
// This is a simple Clients class that
// implements the IPropertyChange interface.
public class Clients: INotifyPropertyChanged
{
// These fields hold the values for the public properties.
private bool isChecked = false;
private string title = String.Empty;
private string first_name = String.Empty;
private string last_name = String.Empty;
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
// The constructor is private to enforce the factory pattern.
private Clients()
{
}
// This is the public factory method.
public static Clients CreateNewCustomer()
{
return new Clients();
}
// This property represents an ID, suitable
// for use as a primary key in a database.
public bool IsChecked
{
get
{
return this.isChecked;
}
set
{
if (value != this.isChecked)
{
this.isChecked = value;
NotifyPropertyChanged();
}
}
}
public string Title
{
get
{
return this.title;
}
set
{
if (value != this.title)
{
this.title = value;
NotifyPropertyChanged();
}
}
}
public string FirstName
{
get
{
return this.first_name;
}
set
{
if (value != this.first_name)
{
this.first_name = value;
NotifyPropertyChanged();
}
}
}
public string LastName
{
get
{
return this.last_name;
}
set
{
if (value != this.last_name)
{
this.last_name = value;
NotifyPropertyChanged();
}
}
}
}
listViewClients.Items.Clear();
for (int a = 0; a < clients.Count; a++)
{
listViewClients.Items.Add(new Clients
{
isChecked = false,
salutation = clients[a].salutation,
title = clients[a].title,
first_name = clients[a].first_name,
last_name = clients[a].last_name,
commercialunitnumber = clients[a].commercialunitnumber,
});
}