2012-04-13 205 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"); 
     } 
    } 
相關問題