WP8突出的SelectedItem LongListSelector [英] WP8 Highlight SelectedItem LongListSelector

查看:152
本文介绍了WP8突出的SelectedItem LongListSelector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关心的是我的LongListSelector突出显示所选项目
当用户点击就可以了。

my concern is to highlight a selected item in my LongListSelector when the user taps on it.

我试过这个解决方案:的http://$c$c.msdn.microsoft.com/windowsapps/Highlight-a-selected-item-30ced444#content

I tried this solution: http://code.msdn.microsoft.com/windowsapps/Highlight-a-selected-item-30ced444#content

但我仍然有一个问题。

在我的项目中,LongListSelector与90〜100个项目填补了,如果我在x 自来水日元素中,(X + 20),第(x + 40)中,(X + 60)中,(X + 80) ...得到突出了。这怎么可能?这是什么原因?



我试着调试,我注意到的是,userControlList(由上面的链接看到MyLongListSelector1_SelectionChanged事件处理函数)具有GetItemsRecursive签署后20个元素,而不是90〜100,因为我至少预期。

But i still have a problem.
In my project, the LongListSelector is filled up with 90~100 items and if i tap on the xth element, the (x+20)th, the (x+40)th, the (x+60)th, the (x+80)th... get highlighted too. How is that possible? What does cause this?

I tried to debug, and what i noticed is that "userControlList" (see the MyLongListSelector1_SelectionChanged event handler by following the link above) has 20 elements after the execution of "GetItemsRecursive", and not 90~100 as i, at least, expected.

如果你解决不了这一点,那么没有人知道如何真正突出LongListSelector选定的项目? (使用列表框,而不是不是一个选项)

If you can't solve this, then does anyone know how to actually highlight selected items in LongListSelector? (using Listbox instead is not an option)

推荐答案

怎么样,我们写信给你一个更好的,但更易于理解?另外,您可以有高亮颜色的组合?我用这几个我的应用程序。它所做的就是其绑定的背景颜色以班级为好。如果选择它返回类的高亮颜色如果没有则返回非高亮颜色。

How about we write you a better one that is much easier to understand? Plus you can have any combination of highlight colors? I use this for a few of my apps. All it does is it binds the background color to the class as well. If it is selected it returns the highlight color of the class if not it returns the non highlight color.

样本数据点 - 因为你可以看到你可以设置高亮颜色和任何高亮显示颜色

Sample Data Point - as you can see you can set a highlight color and a no highlight color

public class sample_data : INotifyPropertyChanged
{
    // Create the OnPropertyChanged method to raise the event
    public event PropertyChangedEventHandler PropertyChanged;         
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    public sample_data(string name)
    {
        this.Name = name;
        this.IsSelected = false;
        this.NonHighlightColor = new SolidColorBrush(Colors.Transparent);
        this.HighLightColor = new SolidColorBrush(Colors.Red);
    }      

    public string Name { get; set; }

    private bool _is_selected;
    public bool IsSelected
    {
        get { return _is_selected; }
        set
        {
            _is_selected = value;
            OnPropertyChanged("HighlightBackgroundColor");
        }
    }

    public SolidColorBrush HighlightBackgroundColor
    {
        get { if (IsSelected) return HighLightColor; else return NonHighlightColor; }
    }

    private SolidColorBrush HighLightColor{ get; set; }

    private SolidColorBrush NonHighlightColor { get; set; }
}


让我们创建的ObservableCollection,设置LongListSelector的的ItemSource。


Lets create the ObservableCollection and set the LongListSelector's ItemSource.

    private ObservableCollection<sample_data> CreateSampleData()
    {
        ObservableCollection<sample_data> sd = new ObservableCollection<sample_data>();

        sd.Add(new sample_data("Bob"));
        sd.Add(new sample_data("Dan"));
        sd.Add(new sample_data("Kate"));
        sd.Add(new sample_data("Bart"));
        sd.Add(new sample_data("Sanders"));
        sd.Add(new sample_data("Dog"));

        return sd;
    }

    // Constructor
    public MainPage()
    {
        InitializeComponent();

        mylonglist.ItemsSource = CreateSampleData();
    }


现在的XAML


Now for the XAML

        <phone:LongListSelector x:Name="mylonglist" SelectionChanged="mylonglist_SelectionChanged">
            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>
                    <StackPanel Background="{Binding HighlightBackgroundColor}" Height="100">
                        <TextBlock Text="{Binding Name}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </phone:LongListSelector.ItemTemplate>
        </phone:LongListSelector>


code的选择更改


Code for the Selection Change

    private void mylonglist_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        try
        {
            LongListSelector ls = sender as LongListSelector;
            sample_data selected_item = ls.SelectedItem as sample_data;

            // unselected the previous selections
            foreach (sample_data sd in ls.ItemsSource)
            {
                if (sd != selected_item)
                {
                    sd.IsSelected = false;
                }
            }

            // set the selected item (this will cause the background color to change)
            selected_item.IsSelected = true;
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
    }


有你有它,现在你可以与任何颜色和每个项目的自定义颜色突出而不凌乱的VisualState经理搞乱。


There you have it, now you can highlight with any colors and with custom colors for each item without messing with the messy VisualState Manager.

这篇关于WP8突出的SelectedItem LongListSelector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆