2012-04-13 203 views
1

我的Windows Phone应用程序中有一个LongListSelector类型的列表。该列表有一个TextBlock和每个项目的复选框。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

你实现DictionaryItem类INotifyPropertyChanged接口? – 2012-04-13 15:04:55

+0

是的,我确实实现了这一点。我用我的代码更新了我的问题。 – 2012-04-16 08:23:50

回答

1

嗯 - 现在我得到它的工作。

我的选择Changed事件现在看起来是这样

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"); 
     } 
    } 
相关问题