Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 无法将样式应用于ResourceDictionary中定义的按钮控件_Wpf_Xaml - Fatal编程技术网

Wpf 无法将样式应用于ResourceDictionary中定义的按钮控件

Wpf 无法将样式应用于ResourceDictionary中定义的按钮控件,wpf,xaml,Wpf,Xaml,我是WPF技术的新手。目前我愿意在WPF应用程序中应用母版页概念 我也用过ResourceDictionary。我在按照以下步骤进行操作时面临一个问题 <Style x:Key="Link" TargetType="Button"> <Setter Property="VerticalAlignment" Value="Center"/>

我是WPF技术的新手。目前我愿意在WPF应用程序中应用母版页概念

我也用过ResourceDictionary。我在按照以下步骤进行操作时面临一个问题

<Style x:Key="Link" TargetType="Button">
                                                <Setter Property="VerticalAlignment" Value="Center"/>
                                                <Setter Property="HorizontalAlignment" Value="Center"/>
                                                <Setter Property="Cursor" Value="Hand"/>
                                                <Setter Property="Foreground" Value="Blue"/>
                                                <Setter Property="Background" Value="Transparent"/>
                                                <Setter Property="Template">
                                                    <Setter.Value>
                                                        <ControlTemplate TargetType="Button">
                                                            <TextBlock TextDecorations="Underline" 
                                                        Text="{TemplateBinding Content}"
                                                        Background="{TemplateBinding Background}"/>
                                                            <ControlTemplate.Triggers>
                                                                <Trigger Property="IsPressed" Value="True">
                                                                    <Setter Property="Foreground" Value="Red"/>
                                                                </Trigger>
                                                            </ControlTemplate.Triggers>
                                                        </ControlTemplate>
                                                    </Setter.Value>
                                                </Setter>
                                            </Style>
我希望链接按钮列表显示在母版页上

由于WPF中没有可用的直接链接按钮控件,我找到了一个用于创建链接按钮的样式脚本,如下所示

<Style x:Key="Link" TargetType="Button">
                                                <Setter Property="VerticalAlignment" Value="Center"/>
                                                <Setter Property="HorizontalAlignment" Value="Center"/>
                                                <Setter Property="Cursor" Value="Hand"/>
                                                <Setter Property="Foreground" Value="Blue"/>
                                                <Setter Property="Background" Value="Transparent"/>
                                                <Setter Property="Template">
                                                    <Setter.Value>
                                                        <ControlTemplate TargetType="Button">
                                                            <TextBlock TextDecorations="Underline" 
                                                        Text="{TemplateBinding Content}"
                                                        Background="{TemplateBinding Background}"/>
                                                            <ControlTemplate.Triggers>
                                                                <Trigger Property="IsPressed" Value="True">
                                                                    <Setter Property="Foreground" Value="Red"/>
                                                                </Trigger>
                                                            </ControlTemplate.Triggers>
                                                        </ControlTemplate>
                                                    </Setter.Value>
                                                </Setter>
                                            </Style>

现在我在ResourceDictionary文件中使用了一个按钮控件,我正在尝试将此样式应用于按钮控件。它成功编译,但运行时显示异常消息

下面是我的资源字典代码

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:MSRTC.Master">

    <Style TargetType="{x:Type local:Master}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:Master}">
                    <Grid Background="LightBlue">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="150"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="150"/>
                        </Grid.ColumnDefinitions>
                        <StackPanel Grid.Column="1">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="200"/>
                                    <ColumnDefinition Width="850"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="120"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <Image Source="D:\Assignments\WPF\MSRTC\Developement\MSRTC\MSRTC\MSRTC\Images\Logo.png" Grid.Column="0" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                                <Image Source="D:\Assignments\WPF\MSRTC\Developement\MSRTC\MSRTC\MSRTC\Images\HeaderImage1.jpg" Grid.Column="1" Grid.Row="0" Stretch="Fill"/>

                                <Grid Grid.Row="1" Grid.Column="0" Background="Silver" Height="400">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="40"/>
                                        <RowDefinition Height="40"/>
                                        <RowDefinition Height="40"/>
                                        <RowDefinition Height="40"/>
                                        <RowDefinition Height="40"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>

                                    <TextBlock Text="Welcome" FontWeight="Bold" FontSize="15" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                                    <Button Name="btnHome" Content="Home" Grid.Row="1" Style="{StaticResource Link}"></Button>

                                </Grid>

                                <StackPanel Margin="10" Grid.Column="1" Grid.Row="1">
                                <!--<StackPanel Margin="10" Grid.Column="0" Grid.Row="1">-->
                                    <ContentPresenter Content="{TemplateBinding Title}"/>
                                    <ContentPresenter Content="{TemplateBinding Abstract}"/>
                                    <ContentControl Content="{TemplateBinding Content}"/>
                                </StackPanel>

                            </Grid>
                            <!--<StackPanel Margin="10">
                                <ContentPresenter Content="{TemplateBinding Title}"/>
                                <ContentPresenter Content="{TemplateBinding Abstract}"/>
                                <ContentControl Content="{TemplateBinding Content}"/>
                            </StackPanel>-->
                        </StackPanel>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

有谁能告诉我哪里出了问题,以及如何应对

还有一件事需要提及,我在App.xaml Application.Resource部分中编写的链接按钮样式脚本


提前谢谢。

有什么例外?(或“内部异常”?)嗨,乔,没有正确的信息。只弹出一个错误消息框,其中包含以下信息PresentationFramework.dll中发生了类型为“System.Windows.Markup.XamlParseException”的未处理异常。其他信息:提供上的值“System.Windows.Markup.StaticResourceHolder”引发异常。如果有此异常的处理程序,程序可以安全地继续。如果单击详细信息,是否存在“内部异常”?嗨,Joe,问题已经解决。我刚刚将StyleStaticResource更改为DynamicResource,它成功了。谢谢你的帮助,乔。祝您今天过得愉快。