使用时无法投射错误

时间:2013-02-28 19:43:05

标签: c# wpf xaml styles

我正在尝试使用资源字典和样式TargetType属性来更改解决方案中按钮的外观。我在加载第一个按钮时在运行时收到一个强制转换错误。如果我在按钮的属性中手动设置样式,一切都工作正常,但重点是改变所有按钮的外观,而不是一次改变一个。

以下是相关的例外情况:

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: 

这是相关代码。 资源字典:

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

主窗口:

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

对于可能导致此问题的任何意见,我将不胜感激。外部错误完全没用,我对此知之甚少,无法理解为什么会发生转换错误。

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

我遇到了类似的问题,结果发现我的风格是使用StaticResource而不是DynamicResource。 http://blog.alner.net/archive/2010/05/07/wpf-style-and-template-resources_order-matters.aspx

答案 2 :(得分:0)

在Xaml代码中,order matters。将模板放在引用它的样式之上。

  

使用WPF资源时,请记住声明资源   正确的顺序。

     

必须定义在别处引用的样式或模板   在它可以使用之前。换句话说,如果Style1使用BrushA作为   必须在Style1之前定义StaticResource,然后定义BrushA。除此以外,   你会得到有趣的错误,如:

     

无法转换类型&#39; MS.Internal.NamedObject&#39;输入   &#39; System.Windows.FrameworkTemplate&#39;