我的 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>
I have implemented this code when selection changes:
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;
}
}
如何在代码中做到这一点?有什么建议么?
更新以匹配评论:
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;
}
}
}