如何在不执行ValueChanged事件的情况下设置ListPicker的索引

时间:2014-11-09 16:05:50

标签: c# xaml windows-phone-8 listpicker

这个问题让我非常疯狂。我有一个ListPicker,动态填充几个项目。我已经在我的页面的Loaded事件中声明了我的SelectionChanged事件处理程序。当用户单击页面上的某个项目时,ListPicker可见性将从Collpased切换为Visible,并设置ListPicker的值。问题是,ListPicker的索引将基于用户设置,因此当前索引中的三个项目可能是1,而不是默认值为0。我需要在ListPicker中显示1作为当前项而不激活SelectionChanged事件(它根据当前索引执行操作)。然后,只有当用户自己更改所选项目时,才需要触发SelectionChanged事件。

这样做的主要原因不仅是用户在显示时需要在ListPicker中查看他或她当前的设置,而是在SelectionChanged事件操作中发生覆盖当前存在的操作,这非常令人困惑且不应该除非用户指定,否则会发生。

我目前拥有的内容如下

XAML

<toolkit:ListPicker x:Name="lp" Visibility="Collapsed" Margin="12" Width="300"/>

XAML.CS

private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        lp.SelectionChanged += lp_SelectionChanged;
    }

void EditableEllipse_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    if (sender != null)
    {
        DependencyObject tappedElement = e.OriginalSource as UIElement;
        // find parent UI element of type PhotoThumbnail
        //PhotoThumbnail i = this.FindParentOfType<PhotoThumbnail>(tappedElement);
        i = this.FindParentOfType<PhotoThumbnail>(tappedElement);
        if (i != null)
        {
            BuildControl(i);
        }
    }
}

private void BuildControl(PhotoThumbnail pp)
    {
        switch(pp.NName)
        {
            case "flip":
                List<ListPickerItem> l = new List<ListPickerItem>();                    
                l.Add(new ListPickerItem { Name = "Flip_Vertical", Content = AppResources.App_Flip_Vertical });
                l.Add(new ListPickerItem { Name = "Flip_Horizontal", Content = AppResources.App_Flip_Horizontal });
                l.Add(new ListPickerItem { Name = "Flip_Both", Content = AppResources.App_Flip_Both });
                lp.ItemsSource = l;  //Code execution jumps from here to ValueChanged event immediately                  
                lp.Visibility = Visibility.Visible;
                lp.SelectedIndex = Settings.Flip.Value - 1;
                break;
        ..
        }
    }

private async void lp_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
            if (lp.SelectedIndex != -1) //always defaults to = 0
            {
                var item = (sender as ListPicker).SelectedItem;
                string name = ((ListPickerItem)item).Name;

                if (name != null)
                {
                        switch (name)
                        {
                            case "Flip_Vertical":
                                Settings.Flip.Value = 1;
                                ..perform process based on current Setting.Flip.Value..                                    break;
                            case "Flip_Horizontal":
                                Settings.Flip.Value = 2;
                                ..perform process based on current Setting.Flip.Value..
                                break;
                            case "Flip_Both":
                                Settings.Flip.Value = 3;
                                ..perform process based on current Setting.Flip.Value..
                                break;

                             ...
                          }
                }
            }

1 个答案:

答案 0 :(得分:0)

尝试使用操作顺序(通过解除钩子并重新加入事件)

private void BuildControl(PhotoThumbnail pp)
{
    switch(pp.NName)
    {
        case "flip":

            // unhook event
            lp.SelectionChanged -= lp_SelectionChanged;

            List<ListPickerItem> l = new List<ListPickerItem>();                    
            l.Add(new ListPickerItem { Name = "Flip_Vertical", Content = AppResources.App_Flip_Vertical });
            l.Add(new ListPickerItem { Name = "Flip_Horizontal", Content = AppResources.App_Flip_Horizontal });
            l.Add(new ListPickerItem { Name = "Flip_Both", Content = AppResources.App_Flip_Both });
            lp.ItemsSource = l;  //Code execution jumps from here to ValueChanged event immediately                  
            lp.Visibility = Visibility.Visible;
            lp.SelectedIndex = Settings.Flip.Value - 1;

            // after setting the index, rehook the event
            lp.SelectionChanged += lp_SelectionChanged;

            break;
    ..
    }
}
相关问题