1

わかりましたので、タブ コントロール テンプレートを変更して 2 つのボタンを追加しました。1 つは開くボタン、もう 1 つは他のタブと共に保存するボタンです。

私がする必要があるのは、ウィンドウ内にある OpenSave/CloseSave 関数をボタンで実行することです。各ウィンドウには、開く機能と保存する機能が異なるため、ウィンドウ内から機能を使用する必要があります。

<Style x:Key="EditorTabControl" TargetType="{x:Type TabControl}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabControl}">
                <Grid SnapsToDevicePixels="True">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="0" />
                        <RowDefinition Height="auto" />
                    </Grid.RowDefinitions>
                    <Border Grid.Row="2" Panel.ZIndex="1" Background="#fafafa" Padding="10" BorderBrush="#ededed" BorderThickness="0 1 0 0">
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                            <Button Content="Open" Style="{StaticResource EditorButtonStyle}"/>
                            <Button Content="Save" Style="{StaticResource EditorButtonStyle}"/>
                            <TabPanel IsItemsHost="True"/>
                        </StackPanel>
                    </Border>
                    <Border Grid.Row="0" BorderThickness="0" BorderBrush="#696969" Background="#FFF">
                        <ContentPresenter Content="{TemplateBinding SelectedContent}" />
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

では、コントロール テンプレートが使用されているウィンドウ内にある関数を実行するにはどうすればよいでしょうか。

4

1 に答える 1

0

コマンドとバインディングを使用してこれを行うことができます。メイン ウィンドウで [保存] および [閉じる] コマンドを設定し、RelativeSource を使用してバインディングを作成できます。次のようなウィンドウを持つことができます。

public partial class MainWindow : Window
{


    public MainWindow()
    {
        InitializeComponent();
    }

    public ICommand Open { get; set; }    //Need to implement, maybe could be a RelayCommand or DelegateCommand, you may search in the internet
    public ICommand Save { get; set; }
}

そして、xaml コードでは:

                     <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                        <Button Content="Open" Style="{StaticResource EditorButtonStyle}" Command="{Open, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
                        <Button Content="Save" Style="{StaticResource EditorButtonStyle}" Command="{Save, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
                        <TabPanel IsItemsHost="True"/>
                    </StackPanel>

これが役立つことを願っています...

于 2013-03-21T20:57:06.717 に答える