Wpf XAML继承

3

在两个不同的窗口中,是否可能继承相同的ControlTemplate样式?我是wpf的新手,不确定如何做到这一点,甚至不知道是否可能实现。 例如,如果在Window1.xaml中有以下内容:

<Window.Resources>
    <ControlTemplate x:Key="myStyle" TargetType="Button">
        ...
    </ControlTemplate>
</Window.Resources>

同时,在Window2.xaml中,我想这样使用它:

<Grid>
    <Grid.Resources>
        <Style TargetType="Button" BasedOn="{StaticResource myStyle}">
            ...
        </Style>
    </Grid.Resources>
<Grid>

如何从第一个窗口导入样式?
1个回答

4

是的,这是可能的。您可以将样式移动到app.xaml中,两个窗口都可以看到该样式。

在app.xaml中可以这样写:

<Application.Resources>
 <ResourceDictionary>
    <Style x:Key="myStyle" TargetType="Button">
        ...
    </Style>
    <Style TargetType="Button" BasedOn="{StaticResource myStyle}">
        ...
    </Style>
 </ResourceDictionary>
</Application.Resources>

两个窗口都会看到这个样式


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接