可以容纳用户控件的动态选项卡控件

Dynamic Tab Control which can hold User Controls

我想创建一个可以容纳多个用户控件的选项卡控件。

<TabControl Padding="0">
    <TabItem Header="{x:Static p:Resources.Scheduler}" 
             Visibility="{Binding ShellService.IsSchedulerEnabled,
                                  Converter={StaticResource BoolToVisibilityConverter}}">
        <ContentControl>
            <ContentControl.Style>
                <Style TargetType="ContentControl">
                    <Style.Triggers>
                        <Trigger Property="IsVisible" Value="True">
                            <Setter Property="Content" 
                                    Value="{Binding ShellService.LazySchedulerView.Value}"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ContentControl.Style>
        </ContentControl>
    </TabItem>
</TabControl>

xaml仅针对1个选项卡项,由ShellService.IsSchedulerEnabled控制,内容为ShellService.LazySchedulerView.Value。

我的问题是,如果我想创建一个新的 TabItem,我必须在 xaml.

中创建一个新的 TabItem 标签

如何在不在 ContentControl 中指定 'Value' 的情况下创建一个动态选项卡控件来容纳 1 个以上的选项卡项。

public interface IShellService : INotifyPropertyChanged
{
    object ShellView { get; }

    bool IsSchedulerEnabled { get; set; }

    Lazy<object> LazySchedulerView { get; set; }
}

[Export(typeof(IShellService)), Export]
internal class ShellService : Model, IShellService
{
    private object _shellView;
    private bool _isSchedulerEnabled;
    private Lazy<object> _lazySchedulerView;

    public object ShellView
    {
        get { return _shellView; }
        set { SetProperty(ref _shellView, value); }
    }

    public bool IsSchedulerEnabled
    {
        get { return _isSchedulerEnabled; }
        set { SetProperty(ref _isSchedulerEnabled, value); }
    }

    public Lazy<object> LazySchedulerView
    {
        get { return _lazySchedulerView; }
        set { SetProperty(ref _lazySchedulerView, value); }
    }
}

您可以将 Style 用于此 TabItem。我为你创建了一些例子。您应该将 Bindings 更改为您自己的。您应该创建 ObservableCollectionShellService 并将其绑定到 TabControl。希望对您有所帮助。

<TabControl ItemsSource="{Binding Objects}">
    <TabControl.Resources>
        <Style TargetType="TabItem" x:Key="{x:Type TabItem}">
            <Setter Property="Header" Value="{Binding Header}"></Setter>
            <Style.Triggers>
                <Trigger Property="IsVisible" Value="True">
                    <Setter Property="Content" Value="{Binding Text}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </TabControl.Resources>
</TabControl>

更新

视图模型示例

public class OwnObject : ViewModelBase
{
    private string _text;

    public string Text
    {
        get { return _text; }
        set { _text = value; NotifyPropertyChanged( "Text" ); }
    }

    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion
    protected void NotifyPropertyChanged( String info )
    {
        if ( PropertyChanged != null )
        {
            PropertyChanged( this, new PropertyChangedEventArgs( info ) );
        }
    }
}

我将这些对象绑定到 TabControl

private ObservableCollection<OwnObject> _objects = new ObservableCollection<OwnObject>();
public ObservableCollection<OwnObject> Objects
{
    get { return _objects; }
    set { _objects = value; NotifyPropertyChanged( "Objects" ); }
}