如何避免_SelectionChanged被解雇?

时间:2014-03-25 07:17:18

标签: .net wpf winforms

按下按钮时,我选择一个用户控件并将ItemsSource设置为null

   CategoriesListBox.ItemsSource = null;

此代码运行后,SelectionChanged事件将立即触发

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>

我需要避免这种情况,并且没有_SelectionChanged被解雇。

知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

试试这个......

private void CategoriesListBox_SelectionChanged(object sender ,System.Windows.Controls.SelectionChangedEventArgs e)
{
    If ( CategoriesListBox.ItemsSource == null)
    {
        some process;
    }
    else
    { 
        some Process;
    }
}

答案 1 :(得分:1)

我用类似的方法解决了这个问题 来源: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;
        }