【发布时间】:2014-08-23 02:35:34
【问题描述】:
我在将 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#-代码:
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,
});
}
这适用于所有其他列,但在尝试绑定 IsChecked 属性时会引发错误。为什么?我得到的错误是德语,但这是〜翻译:
XamlParseException:TwoWay- 或 OneWayToSource-Connections 不适用于写保护属性“isChecked”。
编辑:
好的,我现在发现了错误。我创建了一个名为 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; }
}
然后我只是将填充 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,
});
}
我唯一改变的是 listViewClients.Items.Add(new Clients {...});
【问题讨论】:
-
匿名类型将创建只读属性,
CheckBox默认绑定TwoWay,这是不允许的。创建具有可写属性的正确视图模型类 -
感谢您的评论,它有帮助:-)