我正在vs2010中构建一个项目,它最终将成为我必须编写的一些小项目的模板。 我已成功扩展了一个TextBox控件并将其添加到CustomControl库并为其默认样式创建了一个Resource Dictionary,并将其合并到我的主项目中Generic.xaml并且它可以工作。
当我尝试扩展想法并为要用于设置的标签创建新的资源字典时,问题就出现了。当我尝试在我的主窗口xaml中引用Style时,它抛出以下内容
发生了System.Windows.Markup.XamlParseException Message ='提供值'System.Windows.StaticResourceExtension'引发了一个异常。行号'68'和行位置'48'。 的InnerException: Message =找不到名为“Settings”的资源。资源名称区分大小写。 源= PresentationFramework
所以我可以看到错误,但不知道为什么我会得到它。如果我将样式移动到窗口资源一切都很好。
请有人解释我缺少的东西,请问它是命名空间还是什么?
我的资源词典xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CHCustomControlLibrary1">
<Style TargetType="{x:Type Label}" x:Key="Settings">
<Setter Property="FontSize" Value="16" />
<Setter Property="FontFamily" Value="Arial" />
<Setter Property="Width" Value="90" />
<Setter Property="Height" Value="25" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Top" />
</Style>
我的通用文件位于主题
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CHCustomControlLibrary1">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/CHCustomControlLibrary1;component/ResourceDictionaries/ExTextBoxDictionary.xaml"/>
<ResourceDictionary Source="/CHCustomControlLibrary1;component/ResourceDictionaries/SettingLabelsDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Ellipse}" />
我的主窗口xaml抛出错误
<Label Grid.Column="0" Grid.Row="0"
Content="Property1"
Name="lbProp1"
Style="{StaticResource Settings}"/>
我的完整主窗口xaml
<ControlTemplate x:Key="VerticalExpander" TargetType="{x:Type Expander}">
<Border Name="ContentBorder"
Width="0">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded"
Value="True">
<Setter TargetName="ContentBorder"
Property="Width"
Value="Auto" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="30" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="1" Orientation="Vertical">
<!--<Button Name="B1" Content="T" Click="B1_Click"/>-->
<Button Name="B2"
Click="B2_Click" Height="28" >
<StackPanel>
<Image Source="Icons\tool.ico" />
</StackPanel>
</Button>
</StackPanel>
<DockPanel LastChildFill="True">
<Grid DockPanel.Dock="Right">
<Expander Name="MainExpander2"
Template="{StaticResource VerticalExpander}"
IsExpanded="False"
DockPanel.Dock="Right">
<Border Background="Gray" Width="250" >
<StackPanel Height="348" Name="stackPanel1" Width="250" >
<Label Content="Settings:" Height="28" Name="label1" />
<ScrollViewer Height="275" Name="scrollViewer1" Width="238">
<StackPanel Height="320" HorizontalAlignment="Left" Margin="-3,-4,0,0" Name="stackPanel2" VerticalAlignment="Top" Width="203" Grid.ColumnSpan="2">
<Grid Height="318" Name="grid1" Width="197">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100*" />
<ColumnDefinition Width="100*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30*" />
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0"
Content="Property1"
Name="lbProp1"
Style="{StaticResource Settings}"/>
<MyNamespace:ExTextBox Grid.Column="1" Grid.Row="0"
Name="tbProp1" />
</Grid>
</StackPanel>
</ScrollViewer>
</StackPanel>
</Border>
</Expander>
</Grid>
<StackPanel>
<Border Name="NonSliding"
Width="100"
Height="50"
Background="Green">
</Border>
<Button Content="Test" Height="23" Name="btTestSave" Width="75" Click="btTestSave_Click" />
<Button Content="Test Save" Height="23" Name="button1" Width="75" />
<Border BorderBrush="Red" BorderThickness="2" Height="31" Name="border1" Width="132">
<TextBox Height="23" Name="tbTestSave" Width="120" />
</Border>
</StackPanel>
</DockPanel>
</Grid>