【发布时间】:2012-04-13 09:18:13
【问题描述】:
我的 Windows Phone 应用中有一个 LongListSelector 类型的列表。该列表的每个项目都有一个 TextBlock 和一个 Checkbox。
我有一个绑定,在填充列表时将复选框标记为isChecked,但是当用户更改选择时如何更改复选框的checked 状态?
我的 XAML 如下所示:
<toolkit:LongListSelector Name="DictList" Visibility="Visible" Margin="10,98,10,40" SelectionChanged="DictList_SelectionChanged">
<toolkit:LongListSelector.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Name}" Foreground="Black" FontSize="28" Margin="15,0,0,0" VerticalAlignment="Center"></TextBlock>
<CheckBox VerticalAlignment="Center" HorizontalAlignment="Right" IsChecked="{Binding Checked}" />
</Grid>
</DataTemplate>
</toolkit:LongListSelector.ItemTemplate>
<toolkit:LongListSelector.GroupHeaderTemplate>
<DataTemplate>
<Border BorderBrush="White" Background="White" Padding="10" Margin="0,15,0,15">
<TextBlock Text="{Binding Name}" Foreground="Black" FontSize="32" />
</Border>
</DataTemplate>
</toolkit:LongListSelector.GroupHeaderTemplate>
<toolkit:LongListSelector.GroupItemTemplate>
<DataTemplate>
<Border BorderBrush="White" Background="White" Padding="10" Margin="0,15,0,15">
<TextBlock Text="{Binding Name}" Foreground="Black" FontSize="32" />
</Border>
</DataTemplate>
</toolkit:LongListSelector.GroupItemTemplate>
</toolkit:LongListSelector>
我在选择变化时已经实现了此代码:
private void DictList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
helpers.parrot.DictionaryItem dictItem = this.DictList.SelectedItem as helpers.parrot.DictionaryItem;
if (dictItem != null)
{
dictItem.Checked = false;
}
}
如何在代码中做到这一点?有什么建议吗?
已更新以匹配 cmets:
DictionaryItem 看起来像这样,我在其中实现了 INotifyPropertyChanged 接口
namespace Dict.helpers.parrot
{
public class DictionaryItem : INotifyPropertyChanged
{
public string Name { get; private set; }
public string DictId { get; private set; }
public string MethodId { get; private set; }
private bool checkedValue = true;
public bool Checked {
get
{
return checkedValue;
}
set
{
NotifyPropertyChanged("Checked");
this.checkedValue = value;
}
}
public DictionaryItem(string name, string dictId, string methodId)
{
Name = name;
DictId = dictId;
MethodId = methodId;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
我的 DictionaryCategory 看起来像这样。该对象包含每个 DictionaryItem。
namespace Dict.helpers.parrot
{
public class DictionaryCategory:System.Collections.ObjectModel.ObservableCollection<DictionaryItem>
{
public string Name { get; private set; }
public DictionaryCategory(string categoryName)
{
Name = categoryName;
}
}
}
【问题讨论】:
-
你在 DictionaryItem 类中实现了 INotifyPropertyChanged 接口吗?
-
是的,我确实实现了这一点。我已经用我的代码更新了我的问题。
标签: c# .net xaml windows-phone-7.1 windows-phone