我正在开发一个 WPF 应用程序,该应用程序使用适用于 WPF 的 Telerik RAD 控件。该应用程序将在带有触摸屏的 PC 上使用,因此我需要将 DateTimePicker 控件上的选择器等内容做得更大,以便像我这样拥有香肠手指的人可以轻松按下它们。
我最初使用 Expression Blend 来编辑控件模板的副本。这在我正在设计的 UserControl 的 XAML 文件中创建了一个 ControlTemplate。我现在正在处理另一个 UserControl,它也将使用 DateTimePicker,因此我想重用 ControlTemplate。
我所做的是将修改后的模板移动到项目(WPF 控件库)Generic.XAML 中新命名的 Style 中。这是 Generic.xaml 的一小段:
<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 片段:
<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 中引用您想要引用的命名资源 - 您必须将其放入 app.xaml
您必须在样式的键中指定类型,或者在控件模板的键中指定 ComponentResourceKey 并在类静态构造函数中重写元数据,例如:
public class FlatStylebutton : Button
{
static FlatStylebutton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(FlatStylebutton), new FrameworkPropertyMetadata(typeof(FlatStylebutton)));
}
}
因此,解决您问题的方法可能是将样式移动到 app.xaml
或者,您可以将其放入单独的 ResourceDictionary xaml 文件中,然后将其导入到 ResourceDictionary.MergedDictionaries
关于wpf - 应用程序无法在 Generic.xaml 中找到资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7334445/