0

When pressing a button I select a user control and set ItemsSource to null

   CategoriesListBox.ItemsSource = null;

As soon as this code runs, a SelectionChanged event fires

private void CategoriesListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{}

-

        <Custom:SurfaceListBox
                x:Name="CategoriesListBox"
                ManipulationDelta="CategoriesListBox_ManipulationDelta"
                IsManipulationEnabled="True"
                SelectionChanged="CategoriesListBox_SelectionChanged"
                ItemTemplate="{DynamicResource CategoriesUnselectedDataTemplate}"
                SelectionMode="Single">
        </Custom:SurfaceListBox>

I need to avoid this and have not the _SelectionChanged fired.

Any idea how to solve this?

GibboK
  • 71,848
  • 143
  • 435
  • 658
  • 2
    Remove the _SelectionChanged , simple as that – Sajeetharan Mar 25 '14 at 07:19
  • I need the SelectionChanged when a user click on that Custom:SurfaceListBox – GibboK Mar 25 '14 at 07:20
  • 2
    http://stackoverflow.com/questions/8608128/how-to-cancel-a-combobox-selectionchanged-event – Sajeetharan Mar 25 '14 at 07:23
  • useful http://social.msdn.microsoft.com/Forums/silverlight/en-US/c2b7b3dc-825c-441a-ada1-cc202892211b/how-to-avoid-the-selectionchanged-event-getting-fired-on-pageload-silverlight?forum=silverlightinstall – GibboK Mar 25 '14 at 07:23

2 Answers2

1

try this ...

private void CategoriesListBox_SelectionChanged(object sender ,System.Windows.Controls.SelectionChangedEventArgs e)
{
    If ( CategoriesListBox.ItemsSource == null)
    {
        some process;
    }
    else
    { 
        some Process;
    }
}
Shell
  • 6,818
  • 11
  • 39
  • 70
Malar
  • 163
  • 4
  • 17
  • It would be worth adding some explanation to you answer. – Tom Fenech Mar 25 '14 at 08:34
  • If ( CategoriesListBox.ItemsSource == null) { // write the statement which you want to execute when button click } else { //write the statement which you want to execute when user clicks Custom:surfaceListBox} – Malar Mar 25 '14 at 09:17
1

I solved using a similar approach to this source: http://www.amazedsaint.com/2008/06/wpf-combo-box-cancelling-selection.html

private bool handleSelection=true;

private void ComboBox_SelectionChanged(object sender,
                                        SelectionChangedEventArgs e)
        {
            if (handleSelection)
            {
                MessageBoxResult result = MessageBox.Show
                        ("Continue change?", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.No)
                {
                    ComboBox combo = (ComboBox)sender;
                    handleSelection = false;
                    combo.SelectedItem = e.RemovedItems[0];
                    return;
                }
            }
            handleSelection = true;
        }
GibboK
  • 71,848
  • 143
  • 435
  • 658