防止在 Silverlight 中选择 TabControl

发布于 2024-12-10 08:00:53 字数 198 浏览 0 评论 0原文

有什么方法可以防止 Silverlight 4 中 TabControl 中的选项卡发生更改吗?

一个简单的情况是,当我有一个包含一些数据的表单时,我想询问用户他/她是否想在实际更改选项卡之前保存这些数据。

我见过有关如何在 WPF 中执行此操作的代码示例,但在 Silverlight 中却没有。

我该怎么做才能阻止选项卡发生变化?

Is there any way to prevent the change of a tab in TabControl in Silverlight 4?

A simple case is when I've got a form with some data, and I want to ask the user if he/she wants to save this data before actually changing the tab.

I've seen code examples on how to do this in WPF, but not in Silverlight.

What can I do to stop the tab from changing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的黑色迷你裙 2024-12-17 08:00:53

将 SelectedIndex 绑定到数据上下文上的属性。

<sdk:TabControl SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}">
    <sdk:TabItem Header="TabItem">
        <Grid Background="#FFE5E5E5"/>
    </sdk:TabItem>
    <sdk:TabItem Header="TabItem">
        <Grid Background="#FFE5E5E5"/>
    </sdk:TabItem>
</sdk:TabControl>

在 SET 访问器中,编写代码来检查用户是否确实想要执行他们想要执行的操作。

public class Context : INotifyPropertyChanged
{
    int _SelectedIndex = 0;
    public int SelectedIndex
    {
        get
        {
            return _SelectedIndex;
        }
        set
        {
            MessageBoxResult result = MessageBox.Show("Do you want to save?", "Really?", MessageBoxButton.OKCancel);
            if (result == MessageBoxResult.OK)
            {
                _SelectedIndex = value;
            }
            RaisePropertyChanged("SelectedIndex");
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

最终效果是,如果用户在对话框中选择“取消”,则私有变量永远不会更改 - PropertyChanged 事件触发,将所选索引重新绑定到现有值。

希望这就是您想要实现的目标。

更新 (11/10/2012) - 替代方法(可能适用于 SL5?)。在后面编写代码以绑定到 TabControl 的 SelectionChanged 事件,根据您的测试重置选项卡控件的选定项属性。

    private void TabControl_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        if (e.RemovedItems.Count > 0)
        {
            MessageBoxResult result = MessageBox.Show("Do you want to save?", "Really?", MessageBoxButton.OKCancel);
            if (result != MessageBoxResult.OK)
            {
                ((TabControl)sender).SelectionChanged -= new SelectionChangedEventHandler(TabControl_SelectionChanged);
                ((TabControl)sender).SelectedItem = e.RemovedItems[0];
                ((TabControl)sender).SelectionChanged += new SelectionChangedEventHandler(TabControl_SelectionChanged);
            }
        }
    }

Bind SelectedIndex to a property on your data context.

<sdk:TabControl SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}">
    <sdk:TabItem Header="TabItem">
        <Grid Background="#FFE5E5E5"/>
    </sdk:TabItem>
    <sdk:TabItem Header="TabItem">
        <Grid Background="#FFE5E5E5"/>
    </sdk:TabItem>
</sdk:TabControl>

In the SET accessor, write your code to check whether the user really wants to do what they're trying to do.

public class Context : INotifyPropertyChanged
{
    int _SelectedIndex = 0;
    public int SelectedIndex
    {
        get
        {
            return _SelectedIndex;
        }
        set
        {
            MessageBoxResult result = MessageBox.Show("Do you want to save?", "Really?", MessageBoxButton.OKCancel);
            if (result == MessageBoxResult.OK)
            {
                _SelectedIndex = value;
            }
            RaisePropertyChanged("SelectedIndex");
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

Net effect is, if the user chooses 'cancel' on the dialogue the private variable is never changed - the PropertyChanged event fires, rebinding the selected index to the existing value.

Hope this is what you were trying to accomplish.

UPDATE (11/10/2012) - Alternate method (possibly for SL5?). Write code behind to tie into SelectionChanged event of your TabControl, reset the tab control's selected item property based on your test.

    private void TabControl_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        if (e.RemovedItems.Count > 0)
        {
            MessageBoxResult result = MessageBox.Show("Do you want to save?", "Really?", MessageBoxButton.OKCancel);
            if (result != MessageBoxResult.OK)
            {
                ((TabControl)sender).SelectionChanged -= new SelectionChangedEventHandler(TabControl_SelectionChanged);
                ((TabControl)sender).SelectedItem = e.RemovedItems[0];
                ((TabControl)sender).SelectionChanged += new SelectionChangedEventHandler(TabControl_SelectionChanged);
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文