1

タブ コントロールで wpf ページを開くアプリケーションを作成しています。しかし、タブコントロールで同じページを何度も開くことができます。一度ページを開くと二度と開くことができず、もう一度開こうとするとタブコントロールにフォーカスする必要があります。次のコードを実行しましたが、機能しませんでした。カスタムの closableTabItem Usercontrol を使用しています。

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();
    }
}
4

1 に答える 1

1

ClosableTabItemeverytimeの新しいインスタンスを作成しているため、これは常に一意であるため.Items.Contains、この場合は決して機能しませんobject.Equals

さて、質問で のインスタンスが 1 つだけ必要だと言ったので、 Linq を使用して、アイテムにタイプのClosableTabItemアイテムが存在するかどうかを確認できます。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)))    
...
于 2012-05-25T07:04:27.020 に答える