【发布时间】:2011-09-07 13:12:28
【问题描述】:
我正在开发一个将 Telerik RAD 控件用于 WPF 的 WPF 应用程序。该应用程序将在带有触摸屏的 PC 上使用,因此我需要将 DateTimePicker 控件上的选择器之类的东西放大,以便像我这样的香肠手指的人可以轻松按下它们。
我最初使用 Expression Blend 来编辑控件模板的副本。这在我正在设计的 UserControl 的 XAML 文件中创建了一个 ControlTemplate。我现在正在处理另一个 UserControl,它也将使用 DateTimePicker,所以我想重用 ControlTemplate。
我所做的是将修改后的模板移动到项目(一个 WPF 控件库)Generic.XAML 中的新命名样式中。这是 Generic.xaml 的简短 sn-p:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CarSystem.CustomControls"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:Telerik_Windows_Controls_Chromes="clr-namespace:Telerik.Windows.Controls.Chromes;assembly=Telerik.Windows.Controls">
<Style x:Key="RadDateTimePickerControlTemplate1" TargetType="{x:Type telerik:RadDateTimePicker}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type telerik:RadDateTimePicker}">
. . .
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
这是我引用样式的 XAML 的 sn-p:
<UserControl x:Class="CarSystem.CustomControls.ReportCriteria"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:cs="clr-namespace:CarSystem.CustomControls"
mc:Ignorable="d"
Height="648"
Width="1117">
<Grid Background="{DynamicResource ContentBackground}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<GroupBox BorderBrush="Black" FontSize="20" FontWeight="Bold" Grid.Row="0" Header="Report Criteria: " Margin="5">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="320" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="450" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="110" />
</Grid.ColumnDefinitions>
<GroupBox BorderBrush="Black" FontSize="20" FontWeight="Bold" Grid.Column="0" Header="Date Range:" Margin="5">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock FontSize="18"
FontWeight="Bold"
Grid.Column="0"
Grid.Row="0"
HorizontalAlignment="Right"
Text="Start Date: " />
<telerik:RadDateTimePicker FontSize="18"
FontWeight="Bold"
Grid.Column="1"
Grid.Row="0"
Name="StartDatePicker"
Style="{DynamicResource RadDateTimePickerControlTemplate1}" />
<TextBlock FontSize="18"
FontWeight="Bold"
Grid.Column="0"
Grid.Row="1"
HorizontalAlignment="Right"
Text="End Date: " />
<telerik:RadDateTimePicker FontSize="18"
FontWeight="Bold"
Grid.Column="1"
Grid.Row="1"
Name="EndDatePicker"
Style="{DynamicResource RadDateTimePickerControlTemplate1}" />
</Grid>
</GroupBox>
. . .
</GroupBox>
</Grid>
</UserControl>
当我在 Expression Blend 中工作时,一切看起来都很好。我看到控件的下拉按钮的宽度发生了变化。回到 Visual Studio,一切编译正常,但没有显示更改——我只看到控件的默认样式,并且对样式的引用在它们下面有一条蓝色波浪线。当您将鼠标悬停在波浪线上时,将显示以下消息:
The resource "RadDateTimePickerControlTemplate1" cannot be resolved.
如果我在 XAML 中将“DynamicResource”更改为“StaticResource”,也会发生同样的事情。另外,我没有对项目的 Assembly.Info 文件进行任何更改。
我该如何解决这个问题?
谢谢
托尼
【问题讨论】:
-
generic.xaml 是否在您项目的 Themes 文件夹中?该项目真的是 WPF 库吗?这个库是否作为参考添加到您的项目中?还是您的应用程序中有这个 generic.xaml?您可以尝试通过 ResourceDictionary 在您的用户控件资源中引用您的 generic.xaml。
-
是的,它在项目的 Themes 文件夹中。它真的是一个 WPF 库。是的,它已作为参考添加到我的项目中。该库中有很多用户控件和一些自定义控件,它们在项目的其他地方使用。同一个 Generic.xaml 文件具有应用程序中所有 CustomControls 的默认控件模板,并且工作正常。您能否举例说明“在您的用户控制资源视图 ResourceDictionary 中引用您的 generic.xaml”是什么意思?