1

我的 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;
        }
    }
}
4

1 回答 1

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");
        }
    }
于 2012-04-16T09:18:03.683 回答