WPF 列表视图复选框列绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24528809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Listview Checkboxcolumn Binding
提问by Chris
I'm having trouble binding a Checkboxcolumn to a Listview. What am I doing wrong? XAML-Code:
我在将 Checkboxcolumn 绑定到 Listview 时遇到问题。我究竟做错了什么?XAML 代码:
<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#-Code:
C#-代码:
listViewClients.Items.Clear();
for (int a = 0; a < clients.Count; a++)
{
listViewClients.Items.Add(new
{
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 works fine for all the other Columns, but throws an error when trying to bind the IsChecked property. Why? The Error I get is in German, but here′s the ~ translation:
这适用于所有其他列,但在尝试绑定 IsChecked 属性时会引发错误。为什么?我得到的错误是德语,但这是 ~ 翻译:
XamlParseException: TwoWay- or OneWayToSource-Connections don't work with the write-protected property "isChecked".
XamlParseException:TwoWay- 或 OneWayToSource-Connections 不适用于写保护属性“isChecked”。
EDIT:
编辑:
Ok, I′ve found the error now. I created a class named Clients, here′s the C#-code:
好的,我现在找到了错误。我创建了一个名为 Clients 的类,这是 C# 代码:
class Clients
{
public Boolean isChecked { get; set; }
[JsonProperty("salutation")]
public string salutation { get; set; }
[JsonProperty("title")]
public string title { get; set; }
[JsonProperty("last_name")]
public string last_name { get; set; }
[JsonProperty("first_name")]
public string first_name { get; set; }
[JsonProperty("commercialunitnumber")]
public string commercialunitnumber { get; set; }
}
Then I just changed the C#-Code thats filling the ListView to:
然后我只是将填充 ListView 的 C#-Code 更改为:
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,
});
}
The only thing I changed is listViewClients.Items.Add(new Clients{...});
我唯一改变的是 listViewClients.Items.Add(new Clients{...});
回答by Moez Rebai
Please Create your proper view model class with writable properties because as mentioned "Anonymous type will create read-only properties".
请创建具有可写属性的正确视图模型类,因为如前所述“匿名类型将创建只读属性”。
i added here an example of Clients View model implements INotifyProprtychanged(i didn't add all the property)
我在这里添加了一个客户端视图模型实现的示例INotifyProprtychanged(我没有添加所有属性)
// 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();
}
}
}
}
Then you just need to use it at your list creation:
然后您只需要在创建列表时使用它:
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,
});
}

