C# WPF从模板中向上移动

C# WPF从模板中向上移动,c#,wpf,C#,Wpf,好的,我修改了tab控件模板,添加了两个按钮,一个打开,一个与其他选项卡一起保存 我需要做的是让按钮运行窗口内的OpenSave/CloseSave函数。每个窗口都有自己的打开和保存功能,因为它们会有所不同,这就是为什么我需要它在窗口内使用该功能 <Style x:Key="EditorTabControl" TargetType="{x:Type TabControl}"> <Setter Property="SnapsToDevicePixels" Value="T

好的,我修改了tab控件模板,添加了两个按钮,一个打开,一个与其他选项卡一起保存

我需要做的是让按钮运行窗口内的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>

那么,如何才能使控件模板运行它所使用的窗口中的函数呢?

您可以使用命令和绑定来实现这一点。您可以在主窗口中设置保存和关闭命令,然后使用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>

希望这有助于…

那么我如何才能让控件模板运行它所使用的窗口中的函数呢?绑定到ICommand是否无效?