【问题标题】:How to avoid _SelectionChanged to be fired?如何避免 _SelectionChanged 被解雇?
【发布时间】:2014-03-26 02:24:32
【问题描述】:

按下按钮时,我选择一个用户控件并将 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。

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

【问题讨论】:

标签: .net wpf winforms


【解决方案1】:

试试这个...

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

【讨论】:

  • 值得为您的回答添加一些解释。
  • If ( CategoriesListBox.ItemsSource == null) { // 写下点击按钮时要执行的语句 } else { // 写下用户点击时要执行的语句 Custom:surfaceListBox }
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2019-01-16
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 2013-03-25
    • 2018-11-04
    • 1970-01-01
    相关资源
    最近更新 更多