English 中文(简体)
防止 Tab Control 中页面的多重实例
原标题:Prevent multiple instances of a page in TabControl

我正在用制表符控制程序打开 wpf 页面。 但我可以在制表符控制中一次又一次打开同一个页面。 我希望一旦打开了页面, 它将无法再次打开, 如果我试图再次打开它, 它应该关注制表符控制。 我按照了代码, 但是没有起作用 。 我正在使用自定义的 Closibal TabIot 用户控制 。

private void Set_Fee_Click(object sender, RoutedEventArgs e)
{
    // Adding page to frame and then adding that frame to tab item and then adding tab item to main tab.
    FeeStructure feePage = new FeeStructure();
    _closableTab = new ClosableTabItem();
    _formFrame = new Frame();
    _formFrame.Content = feePage;
    _closableTab.Content = _formFrame;
    _closableTab.Header = "Set Fee Structure";

    if (!mainTab.Items.Contains(_closableTab))
    {
        mainTab.Items.Add(_closableTab);
        _closableTab.Focus();
    }
    else
    {
        _closableTab.Focus();
    }
}

private void Database_RecoveryBackup_Click(object sender, RoutedEventArgs e)
{
    // Adding page to frame and then adding that frame to tab item and then adding tab item to main tab.
    DbRecoveryBackup dbRecBack = new DbRecoveryBackup();
    _closableTab = new ClosableTabItem();
    _formFrame = new Frame();
    _formFrame.Content = dbRecBack;
    _closableTab.Content = _formFrame;
    _closableTab.Header = "Data Base";

    if (!mainTab.Items.Contains(_closableTab))
    {
        mainTab.Items.Add(_closableTab);
        _closableTab.Focus();
    }
    else
    {
        _closableTab.Focus();
    }
}
问题回答

它永远不会发生,你想要什么,因为你每次都在创建一个新的 ClobableTabToproject 实例,因此它每次都是独一无二的,所以 . Projects. contains in this case never works, 因为它符合使用 object.Equals 的项目。

Now, Since you said in question that you only want one instance of ClosableTabItem, then using Linq, you can check if in the items there exist any item of type ClosableTabItem,

...
// Here we re checking the array  Items ,
// if it contains any item whose type is  ClosableTabItem 
if (!mainTab.Items.Any(item => item is ClosableTabItem)))    
...




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签