2011-09-07 84 views
2

我正在爲WPF使用Telerik RAD控件的WPF應用程序。該應用程序將在具有觸摸屏的PC上使用,因此我需要使DateTimePicker控件上的拾取器變得更大,以便像我自己的香腸手指一樣可以輕鬆地按下它們。應用程序無法在Generic.xaml中找到資源

我最初使用Expression Blend編輯控件模板的副本。這在我設計的UserControl的XAML文件中創建了一個ControlTemplate。我有另一個用戶控件,我現在正在使用它也將使用DateTimePicker,所以我想重新使用ControlTemplate。

我所做的是將修改的模板移動到項目(WPF控件庫)Generic.XAML中的新命名樣式中。這裏的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,所有的東西都編譯得很好,但是這個變化並沒有顯示出來 - 我只看到了控件的默認樣式,而且樣式的引用在它們下面有一條藍色的波浪線。當你將鼠標懸停在波浪線,顯示以下信息:如果我在XAML改變「DynamicResource」到「靜態資源」

 The resource "RadDateTimePickerControlTemplate1" cannot be resolved. 

同樣的事情發生。另外,我沒有對該項目的Assembly.Info文件進行更改。

我該如何解決這個問題?

感謝

託尼

+0

是在主題文件夾中的generic.xaml您的項目?該項目真的是WPF庫嗎?並且這個lib是作爲參考添加到您的項目中的嗎? 或者是你的應用程序中的generic.xaml?您可以嘗試通過ResourceDictionary在您的usercontrol資源中引用您的generic.xaml。 – dowhilefor

+0

是的,它位於項目的主題文件夾中。這真是一個WPF庫。是的,它被添加爲我的項目中的參考。該庫中有很多UserControls和一些CustomControls,它們在項目的其他地方使用。相同的Generic.xaml文件具有應用程序中所有CustomControls的默認控制模板,並且工作正常。你能舉一個例子說明你的「在你的usercontrol資源視圖ResourceDictionary中引用你的generic.xaml」是什麼意思? –

回答

3

至於我記得你不能有一個名爲要在generic.xaml引用資源 - 你必須把App.xaml中

你有給在關鍵的類型風格或ComponentResourceKey在鍵CONTROLTEMPLATES 和像類的靜態構造函數覆蓋元數據:

public class FlatStylebutton : Button 
    { 
    static FlatStylebutton() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(FlatStylebutton), new FrameworkPropertyMetadata(typeof(FlatStylebutton))); 
    } 
    } 

所以你的問題的解決方案,可以移動風格的App.xaml

或者你可以把它放在一個單獨的ResourceDictionary XAML文件,並在ResourceDictionary.MergedDictionaries導入

+0

謝謝!將樣式移動到App.xaml中工作。我不得不在其中移動到編譯器喜歡的地方,但它現在正在工作。我想知道它爲什麼在Expression Blend中起作用,如果它在編譯後不起作用,但還不足以解決它。再次感謝! –