TabControl-防止用户更改所选标签:MessageBox的错误造成 [英] TabControl- preventing user from changing the selected tab: MessageBox causing bug

查看:152
本文介绍了TabControl-防止用户更改所选标签:MessageBox的错误造成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经离开冲击在这个问题上一小会儿,并只找到了解决方案的一部分。

I've been pounding away at this issue for a little while, and have only found part of the solution.

我想建立一个TabControl,这样我就可以在某些情况下,防止改变当前选定的选项卡中的用户。当从改变当前选择的标签防止该用户,则它们被示出的对话框。

I'm trying to set up a TabControl so that I can in some cases prevent the user from changing the currently selected tab. When the user is prevented from changing the currently selected tab, then they are shown a dialog box.

我已经阅读了以下文件:

I have already read the following documents:

  • WPF - reset ListBox scroll position when ItemsSource changes
  • http://wizardsofsmart.net/uncategorized/itemssourcechanged-event-using-attached-dependency-properties/
  • http://joshsmithonwpf.wordpress.com/2009/09/04/how-to-prevent-a-tabitem-from-being-selected/
  • http://social.expression.microsoft.com/Forums/en-US/wpf/thread/f7b46018-1e97-4bbe-ada8-49b75dbc1da2/

我已经实现在第三环节表示(尽管上述所有创建下面看到同样的错误)的解决方案。和它的作品, ...

I have implemented the solution indicated in the 3rd link (though all of the above create the same error seen below). And it works, but...

事情搞糟彻底如果用户执行以下操作:

Things mess up thoroughly if the user does the following:


  • 尝试在这样的行动是不允许更改的选项卡。该消息框弹出了错误。

  • 用户点击确定按钮,返回到原来的窗口。

  • 用户再次试图改变标签。 未出现任何消​​息框。

  • 如果用户最小化窗口,然后再最大化了,那么应该在MessageBox出现更早出现。

  • 用户点击确定,返回到原来的窗口... 但标签已改为他们之前选择,即使他们不应该是能够改变标签之一

  • attempts to change the tab when such an action is disallowed. The MessageBox pops up with the error.
  • the user clicks "OK" and is returned to the original window.
  • the user tries again to change the tab. No MessageBox appears.
  • if the user minimizes the window, and then maximizes it again, then the MessageBox that was supposed to appear earlier appears.
  • the user clicks "OK" and is returned to the original window... but the tab has been changed to the one they selected before, even though they should not be able to change tabs.

这是明显的不可以理想的行为。 为什么没有出现在第二次的MessageBox,为什么在标签改变时,它应该这样做?

This is obviously not ideal behavior. Why isn't the MessageBox appearing the second time, and why is the tab changing when it should be disallowed from doing so?

是不允许的。如果我。删除的MessageBox部分,它工作正常。

If I remove the MessageBox part, it works fine.

下面是TabControl.SelectionChanged事件处理程序代码:

Here is the code for the TabControl.SelectionChanged event handler:

bool _isChanging = false;

    private void tabControlForNavigation_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (!_isChanging && canChangeTabs.IsChecked.HasValue)
        {
            _isChanging = true;


            bool canLeave = canChangeTabs.IsChecked.Value;  //normally this would be replaced by a check in the ViewModel

            if (!canLeave)
            {
                int prevIndex = tabControlForNavigation.Items.IndexOf(tabControlForNavigation.SelectedContent);
                tabControlForNavigation.SelectedIndex = prevIndex;
                MessageBox.Show("Can't change tabs!"); //if I comment out this line, everything works fine.
            }

            _isChanging = false;
        }
    }



我使用MVVM来实现这一点。窗口看起来是这样的:

I am using MVVM to implement this. The Window looks like this:

<Window x:Class="TestTabControlSwitching.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"
    Height="350"
    Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <CheckBox x:Name="canChangeTabs"
              Content="Can Change Tabs"
              IsChecked="True" />
    <TabControl x:Name="tabControlForNavigation"
                Grid.Row="1"
                IsSynchronizedWithCurrentItem="True"
                ItemsSource="{Binding Collection}"
                SelectedItem="{Binding SelectedItem}"
                SelectionChanged="tabControlForNavigation_SelectionChanged"
                Margin="4"
                HorizontalAlignment="Stretch">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <ContentPresenter Content="{Binding Path=Name}" />
            </DataTemplate>
        </TabControl.ItemTemplate>

    </TabControl>
</Grid>



我忽略其余为brevity-着想的代码有一个非常直观的视图模型结构的备份窗口。

I'm omitting the rest of the code for sake of brevity- there is a pretty straight-forward ViewModel structure backing the window.

推荐答案

当你注意到了,问题是的MessageBox 的事件处理中。焦点将变为的MessageBox 键,你可以得到所有种类的不良影响。 。我有我自己的这个问题。

As you noticed, the problem is the MessageBox inside the event handler. The focus will change to the MessageBox and you can get all kind of undesired effects. I've had my own problems with this.

下面是关于同一主题的结果
WPF:是否MessageBox的休息PreviewMouseDown 结果
WPF停止时的MessageBox出现路由事件?

Here is a couple of SO question on the same subject
WPF: Does MessageBox Break PreviewMouseDown?
Wpf stop routing event when MessageBox appear?

如果您必须显示一条消息给用户,然后另一种办法是,创建一个新的窗口你的风格像一个的MessageBox 然后调用显示不可以 的ShowDialog )就可以了事件处理中。

If you must display a message to the user then an alternate approach might be to create a new Window which you style like a MessageBox and then call Show (not ShowDialog) on it inside the event handler.

这篇关于TabControl-防止用户更改所选标签:MessageBox的错误造成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆