.net Wpf样式在设计器中应用,但在运行时不应用

.net Wpf样式在设计器中应用,但在运行时不应用,.net,wpf,.net,Wpf,我有三个xaml文件。 Button.xaml(按钮样式) Default.xaml(主题文件)(包括按钮的合并资源文件) MainWindo.xaml(使用default.xaml资源字典) 按钮。xaml: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/w

我有三个xaml文件。

  • Button.xaml(按钮样式)
  • Default.xaml(主题文件)(包括按钮的合并资源文件)
  • MainWindo.xaml(使用default.xaml资源字典)
  • 按钮。xaml:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    <ControlTemplate x:Key="ButtonControlTemplate" TargetType="Button">
        <Border Name="Back" >
            <Border.Background>
                <ImageBrush ImageSource="..\Images\ButtonBackground.png" />
            </Border.Background>
            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" />
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter TargetName="Back" Property="Effect">
                    <Setter.Value>
                        <DropShadowEffect ShadowDepth="0" Direction="0"               
                                                 BlurRadius="15"Color="#D3F80A" Opacity="1"/>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
    
    <Style TargetType="{x:Type Button}">
        <Setter Property="Template" Value="{StaticResource ButtonControlTemplate}" />
    </Style>
    
     </ResourceDictionary>
    
    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:primatives="clr-namespace:System.Windows.Controls;assembly=PresentationFramework">
    
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Controls\Button.xaml"></ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    
    </ResourceDictionary>
    
    <Window x:Class="RemoteDesktop.GUI.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="375" Width="195" WindowStyle="None" Topmost="True" AllowsTransparency="True" Background="Transparent" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" 
            TextOptions.TextFormattingMode="Ideal"
            >
        <Window.Resources>
    
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source=".\Themes\Default\Default.xaml" />
                </ResourceDictionary.MergedDictionaries>
    
    
            </ResourceDictionary>
    
    
        </Window.Resources>
    
        <Border x:Name="border" Margin="10" CornerRadius="10,10,10,10" BorderBrush="#111111" BorderThickness="0">
            <Border.Background>
                <ImageBrush ImageSource=".\Resources\Background.png" Stretch="Fill" />
            </Border.Background>
            <Border.Effect>
                <DropShadowEffect ShadowDepth="0" Direction="0" BlurRadius="15" Color="Black" Opacity="1"/>
            </Border.Effect>
            <Grid  >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition ></ColumnDefinition>
                    <ColumnDefinition Width="58" ></ColumnDefinition>
                    <ColumnDefinition Width="10" ></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="33" />
                    <RowDefinition Height="15"/>
                    <RowDefinition Height="33" />
                    <RowDefinition Height="15"/>
                    <RowDefinition Height="33"/>
                    <RowDefinition Height="23"/>
                    <RowDefinition Height="33"/>
                </Grid.RowDefinitions>
                <Border Background="Transparent" Name="titleBorder" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" MouseLeftButtonDown="titleBorder_MouseLeftButtonDown">
                    <Label Foreground="LightGray" TextOptions.TextHintingMode="Animated" FontSize="14" VerticalAlignment="Center" HorizontalAlignment="Center" >Please Log In</Label>
                </Border>
                <TextBox Grid.Row="2" Grid.Column="0" Width="148" Height="32" Grid.ColumnSpan="3" >
                    Email
                </TextBox>
                <TextBox Grid.Row="4" Grid.Column="0" Width="148" Height="32" Grid.ColumnSpan="3" >
                    Password
                </TextBox>
                <Button  Name="btn"  Grid.Row="6" Grid.Column="1" BorderThickness="0" Foreground="#232323" Content="Sign In" TextOptions.TextHintingMode="Animated" BorderBrush="{x:Null}" >
    
                    <!--<Button.Style>
                        <Style TargetType="Button">
    
                            <Setter Property="OverridesDefaultStyle" Value="True"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type Button}">
                                        <Border Name="Back" >
                                            <Border.Background>
                                                <ImageBrush ImageSource=".\Resources\ButtonBackground.png" />
                                            </Border.Background>
                                            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"  ></TextBlock>
                                        </Border>
                                        <ControlTemplate.Triggers>
                                            <Trigger Property="IsMouseOver" Value="True">
                                                <Setter TargetName="Back" Property="Effect">
                                                    <Setter.Value>
                                                        <DropShadowEffect ShadowDepth="0" Direction="0" BlurRadius="15" Color="#D3F80A" Opacity="1"/>
                                                    </Setter.Value>
                                                </Setter>
                                            </Trigger>
                                        </ControlTemplate.Triggers>
                                    </ControlTemplate>
    
                                </Setter.Value>
                            </Setter>
    
    
                        </Style>
                    </Button.Style>-->
    
                </Button>
            </Grid>
        </Border>
    </Window>
    
    
    
    Default.xaml:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    <ControlTemplate x:Key="ButtonControlTemplate" TargetType="Button">
        <Border Name="Back" >
            <Border.Background>
                <ImageBrush ImageSource="..\Images\ButtonBackground.png" />
            </Border.Background>
            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" />
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter TargetName="Back" Property="Effect">
                    <Setter.Value>
                        <DropShadowEffect ShadowDepth="0" Direction="0"               
                                                 BlurRadius="15"Color="#D3F80A" Opacity="1"/>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
    
    <Style TargetType="{x:Type Button}">
        <Setter Property="Template" Value="{StaticResource ButtonControlTemplate}" />
    </Style>
    
     </ResourceDictionary>
    
    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:primatives="clr-namespace:System.Windows.Controls;assembly=PresentationFramework">
    
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Controls\Button.xaml"></ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    
    </ResourceDictionary>
    
    <Window x:Class="RemoteDesktop.GUI.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="375" Width="195" WindowStyle="None" Topmost="True" AllowsTransparency="True" Background="Transparent" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" 
            TextOptions.TextFormattingMode="Ideal"
            >
        <Window.Resources>
    
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source=".\Themes\Default\Default.xaml" />
                </ResourceDictionary.MergedDictionaries>
    
    
            </ResourceDictionary>
    
    
        </Window.Resources>
    
        <Border x:Name="border" Margin="10" CornerRadius="10,10,10,10" BorderBrush="#111111" BorderThickness="0">
            <Border.Background>
                <ImageBrush ImageSource=".\Resources\Background.png" Stretch="Fill" />
            </Border.Background>
            <Border.Effect>
                <DropShadowEffect ShadowDepth="0" Direction="0" BlurRadius="15" Color="Black" Opacity="1"/>
            </Border.Effect>
            <Grid  >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition ></ColumnDefinition>
                    <ColumnDefinition Width="58" ></ColumnDefinition>
                    <ColumnDefinition Width="10" ></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="33" />
                    <RowDefinition Height="15"/>
                    <RowDefinition Height="33" />
                    <RowDefinition Height="15"/>
                    <RowDefinition Height="33"/>
                    <RowDefinition Height="23"/>
                    <RowDefinition Height="33"/>
                </Grid.RowDefinitions>
                <Border Background="Transparent" Name="titleBorder" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" MouseLeftButtonDown="titleBorder_MouseLeftButtonDown">
                    <Label Foreground="LightGray" TextOptions.TextHintingMode="Animated" FontSize="14" VerticalAlignment="Center" HorizontalAlignment="Center" >Please Log In</Label>
                </Border>
                <TextBox Grid.Row="2" Grid.Column="0" Width="148" Height="32" Grid.ColumnSpan="3" >
                    Email
                </TextBox>
                <TextBox Grid.Row="4" Grid.Column="0" Width="148" Height="32" Grid.ColumnSpan="3" >
                    Password
                </TextBox>
                <Button  Name="btn"  Grid.Row="6" Grid.Column="1" BorderThickness="0" Foreground="#232323" Content="Sign In" TextOptions.TextHintingMode="Animated" BorderBrush="{x:Null}" >
    
                    <!--<Button.Style>
                        <Style TargetType="Button">
    
                            <Setter Property="OverridesDefaultStyle" Value="True"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type Button}">
                                        <Border Name="Back" >
                                            <Border.Background>
                                                <ImageBrush ImageSource=".\Resources\ButtonBackground.png" />
                                            </Border.Background>
                                            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"  ></TextBlock>
                                        </Border>
                                        <ControlTemplate.Triggers>
                                            <Trigger Property="IsMouseOver" Value="True">
                                                <Setter TargetName="Back" Property="Effect">
                                                    <Setter.Value>
                                                        <DropShadowEffect ShadowDepth="0" Direction="0" BlurRadius="15" Color="#D3F80A" Opacity="1"/>
                                                    </Setter.Value>
                                                </Setter>
                                            </Trigger>
                                        </ControlTemplate.Triggers>
                                    </ControlTemplate>
    
                                </Setter.Value>
                            </Setter>
    
    
                        </Style>
                    </Button.Style>-->
    
                </Button>
            </Grid>
        </Border>
    </Window>
    
    
    
    MainWindow.xaml:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    <ControlTemplate x:Key="ButtonControlTemplate" TargetType="Button">
        <Border Name="Back" >
            <Border.Background>
                <ImageBrush ImageSource="..\Images\ButtonBackground.png" />
            </Border.Background>
            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" />
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter TargetName="Back" Property="Effect">
                    <Setter.Value>
                        <DropShadowEffect ShadowDepth="0" Direction="0"               
                                                 BlurRadius="15"Color="#D3F80A" Opacity="1"/>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
    
    <Style TargetType="{x:Type Button}">
        <Setter Property="Template" Value="{StaticResource ButtonControlTemplate}" />
    </Style>
    
     </ResourceDictionary>
    
    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:primatives="clr-namespace:System.Windows.Controls;assembly=PresentationFramework">
    
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Controls\Button.xaml"></ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    
    </ResourceDictionary>
    
    <Window x:Class="RemoteDesktop.GUI.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="375" Width="195" WindowStyle="None" Topmost="True" AllowsTransparency="True" Background="Transparent" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" 
            TextOptions.TextFormattingMode="Ideal"
            >
        <Window.Resources>
    
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source=".\Themes\Default\Default.xaml" />
                </ResourceDictionary.MergedDictionaries>
    
    
            </ResourceDictionary>
    
    
        </Window.Resources>
    
        <Border x:Name="border" Margin="10" CornerRadius="10,10,10,10" BorderBrush="#111111" BorderThickness="0">
            <Border.Background>
                <ImageBrush ImageSource=".\Resources\Background.png" Stretch="Fill" />
            </Border.Background>
            <Border.Effect>
                <DropShadowEffect ShadowDepth="0" Direction="0" BlurRadius="15" Color="Black" Opacity="1"/>
            </Border.Effect>
            <Grid  >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition ></ColumnDefinition>
                    <ColumnDefinition Width="58" ></ColumnDefinition>
                    <ColumnDefinition Width="10" ></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="33" />
                    <RowDefinition Height="15"/>
                    <RowDefinition Height="33" />
                    <RowDefinition Height="15"/>
                    <RowDefinition Height="33"/>
                    <RowDefinition Height="23"/>
                    <RowDefinition Height="33"/>
                </Grid.RowDefinitions>
                <Border Background="Transparent" Name="titleBorder" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" MouseLeftButtonDown="titleBorder_MouseLeftButtonDown">
                    <Label Foreground="LightGray" TextOptions.TextHintingMode="Animated" FontSize="14" VerticalAlignment="Center" HorizontalAlignment="Center" >Please Log In</Label>
                </Border>
                <TextBox Grid.Row="2" Grid.Column="0" Width="148" Height="32" Grid.ColumnSpan="3" >
                    Email
                </TextBox>
                <TextBox Grid.Row="4" Grid.Column="0" Width="148" Height="32" Grid.ColumnSpan="3" >
                    Password
                </TextBox>
                <Button  Name="btn"  Grid.Row="6" Grid.Column="1" BorderThickness="0" Foreground="#232323" Content="Sign In" TextOptions.TextHintingMode="Animated" BorderBrush="{x:Null}" >
    
                    <!--<Button.Style>
                        <Style TargetType="Button">
    
                            <Setter Property="OverridesDefaultStyle" Value="True"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type Button}">
                                        <Border Name="Back" >
                                            <Border.Background>
                                                <ImageBrush ImageSource=".\Resources\ButtonBackground.png" />
                                            </Border.Background>
                                            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"  ></TextBlock>
                                        </Border>
                                        <ControlTemplate.Triggers>
                                            <Trigger Property="IsMouseOver" Value="True">
                                                <Setter TargetName="Back" Property="Effect">
                                                    <Setter.Value>
                                                        <DropShadowEffect ShadowDepth="0" Direction="0" BlurRadius="15" Color="#D3F80A" Opacity="1"/>
                                                    </Setter.Value>
                                                </Setter>
                                            </Trigger>
                                        </ControlTemplate.Triggers>
                                    </ControlTemplate>
    
                                </Setter.Value>
                            </Setter>
    
    
                        </Style>
                    </Button.Style>-->
    
                </Button>
            </Grid>
        </Border>
    </Window>
    
    
    请登录
    电子邮件
    密码
    
    在上面的窗口中,按钮样式在设计时应用,但在运行时会重置为默认状态。

    正如我看到的那样

    rootdirectory\controls\button.xaml

    rootdirectory\themes\default\default.xaml

    rootdirectory\mainwindow.xaml

    这就是问题所在

    在default.xaml中,您必须转到后面的目录才能访问按钮控件,如下所示

    <ResourceDictionary Source="..\..\Controls\Button.xaml"></ResourceDictionary>
    

    别忘了清理项目Snoop()告诉您按钮模板的来源是什么?