WPF OnMouse Up from Template

时间:2013-03-21 17:43:41

标签: c# wpf

好的,所以我修改了标签控件模板,添加了两个按钮,一个打开,另一个与其他标签一起保存。

我需要做的是让按钮运行在窗口内的OpenSave / CloseSave函数。每个窗口都有自己的打开和保存功能,因为它们会有所不同,这就是为什么我需要它来使用Window中的功能。

<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>

那么我怎么能让控件模板运行一个在它使用的Window中的函数呢?

1 个答案:

答案 0 :(得分:0)

您可以使用命令和绑定来执行此操作。您可以在主窗口中设置“保存”和“关闭”命令,然后使用RelativeSource来生成bindng。你可以有这样一个窗口:

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>

希望这可以帮助......