我的Windows Phone应用程序中有一个LongListSelector类型的列表。该列表为每个项目都有一个TextBlock和一个复选框。
我有一个绑定,在填充列表时将复选框标记为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;
}
}
如何在代码中执行此操作?有什么建议吗?
已更新以匹配评论:
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;
}
}
}
答案 0 :(得分:1)
好吧 - 现在我开始工作了。
我的选择更改事件现在看起来像这样
private void DictList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
helpers.parrot.DictionaryItem dictItem = this.DictList.SelectedItem as helpers.parrot.DictionaryItem;
if (dictItem != null)
{
dictItem.Checked = !dictItem.Checked;
}
LongListSelector _sender = (LongListSelector)sender;
_sender.SelectedItem = -1;
}
在DictionaryItem中我更改了以下内容
public bool Checked {
get
{
return checkedValue;
}
set
{
NotifyPropertyChanged("Checked");
this.checkedValue = value;
}
}
到
public bool Checked {
get
{
return checkedValue;
}
set
{
this.checkedValue = value;
NotifyPropertyChanged("Checked");
}
}