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;
}
}
コードでこれを行う方法は?助言がありますか?
コメントに合わせて更新:
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;
}
}
}