WP7 Toolkit. How to ignore events such as ListPicker "SelectionChanged" and ToggleSwitch "Checked" events when the code changes (rather than user input)?

I am new to XAML and C #, but I enjoyed working after a couple of weeks when I played with it. I started working on the application and put together a simple "Settings" page in XAML; Now I am trying to connect events to the controls in order to (a) update the state of the application when the user interacts with them, and (b) have the current state when visiting the page.

I hit two (connected) road blocks, though:

  • Toolkit: The ListPicker control does not work when I define "ListPickerItem" in XAML, so in the SettingsPage constructor I set the contents manually:

    lpColour.ItemsSource = new List<string>()
    {
        "Red","Blue","Green","Custom…"
    };
    lpColour.SelectedIndex = 1;  // set the currently selected item to "Blue"
    

    However, since the control (lpColour in this example) has an event on SelectionChanged, two events are fired (one with "red" selected when the window is filled, and then the other when "Blue" is selected). I do not want to handle "SelectionChanged" at the moment; only when the user interacts with the control itself (for example, if they select "Custom ...", I can open a separate text block and give it focus, but I do not want to do this when I configure and they have selected "Custom ... "because otherwise the user will receive the keyboard as soon as they open the settings page ...)

  • , , ToggleSwitch "" "", "IsChecked" - . , ? ( , "", , ).

, , - "" (, "" " " ) "SelectionChangedEventArgs" "RoutedEventArgs"... , , ?

"" bool ( "false" , "true" Constructor "if (initialized) {...}", , "lpColour.ItemSource =..." "lpColour.SelectedIndex = 1", , "initialized" "false". .: P

, - !

, . !

- @MyKuLLSKI, .

, , " " IgnoreSelectionChanged " int, " "( ListPicker ItemSource " IgnoreSelectionChanged + = 2 "( , ), , " IgnoreSelectionChanged ++ " SelectedIndex ..., .

"ObservableCollection", ListPicker , , , , "SelectionChanged" ListPicker, .

!

+5
1

/.

  • , ItemSource XAML, , , Binding. Bindings DataContext Binding UIElement.

  • -, , DependencyProperty INotifyPropertyChanged

  • ListPicker. , , ObservableCollextion(). , ListPicker, , ListPicker .

  • , SelectionChanged 2 , , . ListPicker , null -1, . , ItemSource, SelectedIndex 0, 1.

  • - , , ,

  • Silverlight IsLoaded, bool, true.

  • Binding UIElement. , .

, (WP7.1):

XAML

  <phone:PhoneApplicationPage 
       x:Class="WP7Sandbox.MainPage"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
       xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
       xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
       DataContext="{Binding RelativeSource={RelativeSource Self}}"
       mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
       FontFamily="{StaticResource PhoneFontFamilyNormal}"
       FontSize="{StaticResource PhoneFontSizeNormal}"
       Foreground="{StaticResource PhoneForegroundBrush}"
       SupportedOrientations="Portrait" Orientation="Portrait"
       shell:SystemTray.IsVisible="True"
       Loaded="PhoneApplicationPageLoaded">

    <Grid>
        <StackPanel>
            <toolkit:ListPicker ItemsSource="{Binding ListPickerCollection, Mode=TwoWay}" SelectionChanged="ListPickerSelectionChanged" SelectedIndex="{Binding ListPickerSelectedIndex, Mode=TwoWay}"/>
            <Button Click="ButtonClick" Content="Selection Change and Ignore Event"/>
            <Button Click="Button2Click" Content="Selection Change and Trigger Event"/>

            <toolkit:ToggleSwitch IsChecked="{Binding ToggleSwitchValue, Mode=TwoWay}"/>
        </StackPanel>
    </Grid>
</phone:PhoneApplicationPage>

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;

namespace WP7Sandbox
{
    public partial class MainPage : PhoneApplicationPage, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

        }

        private bool IsLoaded;

        private bool IgnoreSelectionChanged;

        public ObservableCollection<string> ListPickerCollection { get; private set; }

        private bool _ToggleSwitchValue;
        public bool ToggleSwitchValue
        {
            get
            {
                 return _ToggleSwitchValue;
            }

            set
            {

                _ToggleSwitchValue = value;
                OnPropertyChanged("ToggleSwitchValue");
            }
        }

        private int _ListPickerSelectedIndex;
        public int ListPickerSelectedIndex
        {
            get
            {
                return _ListPickerSelectedIndex;
            } 

            set
            {
                _ListPickerSelectedIndex = value;
                OnPropertyChanged("ListPickerSelectedIndex");
            }
        }

        public MainPage()
        {
            InitializeComponent();

            ListPickerCollection = new ObservableCollection<string>()
            {
                "Red",
                "Blue",
                "Green",
                "Custom…"
            };
        }

        private void PhoneApplicationPageLoaded(object sender, RoutedEventArgs e)
        {
            IsLoaded = true;
        }

        private void ListPickerSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (IsLoaded && !IgnoreSelectionChanged)
            {
            }

            IgnoreSelectionChanged = false;
        }

        private void ButtonClick(object sender, RoutedEventArgs e)
        {
            // I want to ignore this SelectionChanged Event
            IgnoreSelectionChanged = true;
            ChangeListPickerSelectedIndex();
        }

        private void Button2Click(object sender, RoutedEventArgs e)
        {
            // I want to trigger this SelectionChanged Event
            IgnoreSelectionChanged = false; // Not needed just showing you
            ChangeListPickerSelectedIndex();
        }

        private void ChangeListPickerSelectedIndex()
        {
            if (ListPickerSelectedIndex - 1 < 0)
                ListPickerSelectedIndex = ListPickerCollection.Count - 1;

            else
                ListPickerSelectedIndex--;
        }
    }
}

,

+6

All Articles