3

I'm attempting to use a resource dictionary and the style TargetType property to change the look of the buttons in my solution. I am receiving a cast error at run time when the first button is loaded. Everything is working fine if I manually set the style in the button's properties, but the point is to change the look of all the buttons, not one at a time.

Here's the relevant exception:

System.Windows.Markup.XamlParseException occurred
  HResult=-2146233087
  Message='Initialization of 'System.Windows.Controls.Button' threw an exception.' Line number '6' and line position '72'.
  Source=PresentationFramework
  LineNumber=6
  LinePosition=72
  StackTrace:
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
       at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
       at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       at WpfTestApplication.Views.MainWindow.InitializeComponent() in d:\TFSC\CUDL\Research\Chris.Dailey\WpfTestApplication\WpfTestApplication\Views\MainWindow.xaml:line 1
       at WpfTestApplication.Views.MainWindow..ctor() in d:\TFSC\CUDL\Research\Chris.Dailey\WpfTestApplication\WpfTestApplication\Views\MainWindow.xaml.cs:line 28
  InnerException: System.InvalidCastException
       HResult=-2147467262
       Message=Unable to cast object of type 'MS.Internal.NamedObject' to type 'System.Windows.FrameworkTemplate'.
       Source=PresentationFramework
       StackTrace:
            at System.Windows.Controls.Control.OnTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
            at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
            at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
            at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
            at System.Windows.StyleHelper.ApplyStyleOrTemplateValue(FrameworkObject fo, DependencyProperty dp)
            at System.Windows.StyleHelper.InvalidateContainerDependents(DependencyObject container, FrugalStructList`1& exclusionContainerDependents, FrugalStructList`1& oldContainerDependents, FrugalStructList`1& newContainerDependents)
            at System.Windows.StyleHelper.DoStyleInvalidations(FrameworkElement fe, FrameworkContentElement fce, Style oldStyle, Style newStyle)
            at System.Windows.StyleHelper.UpdateStyleCache(FrameworkElement fe, FrameworkContentElement fce, Style oldStyle, Style newStyle, Style& styleCache)
            at System.Windows.FrameworkElement.OnStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
            at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
            at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
            at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
            at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty   dp, Boolean preserveCurrentValue)
            at System.Windows.FrameworkElement.UpdateStyleProperty()
            at System.Windows.FrameworkElement.OnInitialized(EventArgs e)
            at System.Windows.FrameworkElement.TryFireInitialized()
            at System.Windows.FrameworkElement.EndInit()
            at MS.Internal.Xaml.Runtime.ClrObjectRuntime.InitializationGuard(XamlType xamlType, Object obj, Boolean begin)
       InnerException: 

Here's the relevant code. The Resource Dictionary:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">    <Style TargetType="{x:Type Button}">
    <Style TargetType="{x:Type Button}">
        <Setter Property="Control.Template" Value="{StaticResource ButtonTemplate}"
    </Style>
    <ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}">
        <Border Name="Border" BorderBrush="Orange" BorderThickness="3" CornerRadius="3" Background="Red" TextBlock.Foreground="White">
            <Grid>
                <Rectangle Name="FocusCue" Visibility="Hidden" Stroke="Black" StrokeThickness="1" StrokeDashArray="1 2" SnapsToDevicePixels="True" ></Rectangle>
                <ContentPresenter RecognizesAccessKey="True" Margin="{TemplateBinding Padding}"></ContentPresenter>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter TargetName="Border" Property="Background" Value="DarkRed" />
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Setter TargetName="Border" Property="Background" Value="IndianRed" />
                <Setter TargetName="Border" Property="BorderBrush" Value="DarkKhaki" />
            </Trigger>
            <Trigger Property="IsKeyboardFocused" Value="True">
                <Setter TargetName="FocusCue" Property="Visibility" Value="Visible" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter TargetName="Border" Property="TextBlock.Foreground" Value="Gray" />
                <Setter TargetName="Border" Property="Background" Value="MistyRose" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</ResourceDictionary>

The main window:

<Window x:Class="WpfTestApplication.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<WrapPanel>
    <Button x:Name="ButtonGradient" Click="ButtonGradient_OnClick">Gradient</Button>
    <Button x:Name="ButtonDrawing" Click="ButtonDrawing_OnClick">Drawing</Button>
    <Button x:Name="ButtonEventTester"  Click="ButtonEventTester_OnClick">Event Tester</Button>
    <Button x:Name="ButtonCommandTester"  Style="{x:Null}" Command="New">Execute a Command</Button>
</WrapPanel>

I would appreciate any input on what might be causing this. The outer error is completely useless, and I don't know enough about this to understand why a casting error might be occuring.

4

3 に答える 3

3

VS2012 と上記のスタイルを使用して、ControlTemplate を正常に適用できました。最初にラップしましたStyleが、それは好みの問題です。

他の唯一の違いは、それが含まれていなかったため、あなたがあなたのために何をしたかを推測しなければならなかったことですBorder.

以下のスタイルは、設計時と実行時の両方で問題なく機能しました。

<Style TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Name="Border" Background="LightBlue">
                    <Grid>
                        <Rectangle Name="FocusCue" Visibility="Hidden" Stroke="Black" StrokeThickness="1" StrokeDashArray="1 2" SnapsToDevicePixels="True" ></Rectangle>
                        <ContentPresenter RecognizesAccessKey="True" Margin="{TemplateBinding Padding}"></ContentPresenter>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="DarkRed" />
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="IndianRed" />
                        <Setter TargetName="Border" Property="BorderBrush" Value="DarkKhaki" />
                    </Trigger>
                    <Trigger Property="IsKeyboardFocused" Value="True">
                        <Setter TargetName="FocusCue" Property="Visibility" Value="Visible" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="Border" Property="TextBlock.Foreground" Value="Gray" />
                        <Setter TargetName="Border" Property="Background" Value="MistyRose" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
于 2013-02-28T20:19:23.800 に答える