2012-02-23 20 views
2

App.xamlをクリーンアップし、いくつかのスタイルをGeneric.xamlに移動しました。しかし、これらのスタイルは効果がありません。どうして? App.xamlをきれいにするためにこれが正しいのでしょうか?Generic.xamlの私のスタイルが機能しないのはなぜですか?

EDIT(いくつかのソースコードを含める):

目標は、上から下へと(デフォルトのスタイルは、行と列ヘッダを除く)左から右に実行スクロールバーを作るためにWPFデータグリッドのスタイルを変更することです。

スタイルはApp.xamlに入れても機能しています。これはコードの大部分ですから、App.xamlから/Themes/DataGridEx.xamlに移動したいと思います。ところで、DataGridExはWPF DataGridから派生した私の拡張クラスです。

これは私のDataGrid.xamlです:

<ControlTemplate x:Key="SelectAllButtonTemplate" TargetType="{x:Type Button}"> 
    <Grid> 
     <Rectangle x:Name="Border" 
      Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" 
      SnapsToDevicePixels="True" /> 
     <Polygon x:Name="Arrow" 
      HorizontalAlignment="Right" 
      VerticalAlignment="Bottom" 
      Margin="8,8,3,3" 
      Opacity="0.15" 
      Fill="Black" 
      Stretch="Uniform" 
      Points="0,10 10,10 10,0" /> 
    </Grid> 
    <ControlTemplate.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter TargetName="Border" Property="Stroke" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" /> 
     </Trigger> 
     <Trigger Property="IsPressed" Value="True"> 
      <Setter TargetName="Border" Property="Fill" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" /> 
     </Trigger> 
     <Trigger Property="IsEnabled" Value="False"> 
      <Setter TargetName="Arrow" Property="Visibility" Value="Collapsed" /> 
     </Trigger> 
    </ControlTemplate.Triggers> 
</ControlTemplate> 

<Style TargetType="{x:Type local:DataGridEx}" x:Key="{x:Type local:DataGridEx}"> 
    <Setter Property="Background" 
      Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
    <Setter Property="Foreground" 
      Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
    <Setter Property="BorderBrush" Value="#FF688CAF" /> 
    <Setter Property="BorderThickness" Value="1" /> 
    <Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected" /> 
    <Setter Property="ScrollViewer.CanContentScroll" 
      Value="true"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:DataGridEx}"> 
       <Border Background="{TemplateBinding Background}" 
       BorderBrush="{TemplateBinding BorderBrush}" 
       BorderThickness="{TemplateBinding BorderThickness}" 
       SnapsToDevicePixels="True" 
       Padding="{TemplateBinding Padding}"> 
        <ScrollViewer Focusable="false" 
         Name="DG_ScrollViewer"> 
         <ScrollViewer.Template> 
          <ControlTemplate TargetType="{x:Type ScrollViewer}"> 
           <Grid> 
            <Grid.RowDefinitions> 
             <RowDefinition Height="Auto"/> 
             <RowDefinition Height="*"/> 
             <RowDefinition Height="Auto"/> 
            </Grid.RowDefinitions> 

            <Grid.ColumnDefinitions> 
             <ColumnDefinition Width="Auto"/> 
             <ColumnDefinition Width="*"/> 
             <ColumnDefinition Width="Auto"/> 
            </Grid.ColumnDefinitions> 

            <!--Left Column Header Corner --> 
            <Button Command="{x:Static local:DataGridEx.SelectAllCommand}" 
         Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:DataGridEx}}, Path=CellsPanelHorizontalOffset}" 
         Template="{StaticResource SelectAllButtonTemplate}" 
         Focusable="false" 
         Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:DataGridEx}}, Path=HeadersVisibility, Converter={x:Static local:DataGridEx.HeadersVisibilityConverter}, ConverterParameter={x:Static DataGridHeadersVisibility.All}}" /> 
            <!--Column Headers--> 
            <DataGridColumnHeadersPresenter Grid.Column="1" 
                x:Name="PART_ColumnHeadersPresenter" 
                Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:DataGridEx}}, Path=HeadersVisibility, Converter={x:Static DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static DataGridHeadersVisibility.Column}}"/> 

            <!--DataGrid content--> 
            <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" Grid.Row="1" Grid.ColumnSpan="2" CanContentScroll="{TemplateBinding CanContentScroll}" /> 

            <!-- Changed Grid.Row="1" to Grid.Row="0" Grid.RowSpan="2" to make the scrollbar start from top --> 
            <ScrollBar Grid.Row="0" Grid.RowSpan="2" Grid.Column="2" Name="PART_VerticalScrollBar" 
             Orientation="Vertical" 
             Maximum="{TemplateBinding ScrollableHeight}" 
             ViewportSize="{TemplateBinding ViewportHeight}" 
             Value="{Binding Path=VerticalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}" 
             Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/> 

            <!--Grid Grid.Row="2" Grid.Column="1"> 
        <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type dg:DataGrid}}, Path=NonFrozenColumnsViewportHorizontalOffset}"/> 
        <ColumnDefinition Width="*"/> 
        </Grid.ColumnDefinitions> 
        <ScrollBar Grid.Column="1" 
          Name="PART_HorizontalScrollBar" 
          Orientation="Horizontal" 
          Maximum="{TemplateBinding ScrollableWidth}" 
          ViewportSize="{TemplateBinding ViewportWidth}" 
          Value="{Binding Path=HorizontalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}" 
          Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/> 

       </Grid--> 
            <!-- Make the scrollbar to start from left edge --> 
            <ScrollBar Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" 
              Name="PART_HorizontalScrollBar" 
              Orientation="Horizontal" 
              Maximum="{TemplateBinding ScrollableWidth}" 
              ViewportSize="{TemplateBinding ViewportWidth}" 
              Value="{Binding Path=HorizontalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}" 
              Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/> 
           </Grid> 
          </ControlTemplate> 
         </ScrollViewer.Template> 
         <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 
        </ScrollViewer> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="IsGrouping" Value="true"> 
      <Setter Property="ScrollViewer.CanContentScroll" Value="false"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

これが私のテーマ/ Generic.xamlです:

<ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="/Themes/DataGridEx.xaml"/> 
    <ResourceDictionary Source="/Themes/GridComboBox.xaml"/> 
</ResourceDictionary.MergedDictionaries> 

これは私のApp.xamlです:このコードで

<Application x:Class="MyApp.App" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:local="clr-namespace:MyApp.Common" 
       DispatcherUnhandledException="Application_DispatcherUnhandledException"> 
       <!--StartupUri="MainWindow.xaml"--> 
     <Application.Resources> 
      <ResourceDictionary x:Key="rd"> 
       <ResourceDictionary.MergedDictionaries> 
        <ResourceDictionary Source="Themes/Generic.xaml"/> 
       </ResourceDictionary.MergedDictionaries> 
      </ResourceDictionary> 
.... 
     </Application.Resources> 
    </Application> 

、私は、実行時XamlParseExceptionを得た:「『:DataGridExローカル』のテキストから 『種類』を作成できませんでした」。しかし、私がApp.xamlのMergedDictionariesをコメントアウトすると、それは文句はありませんが、スタイルは有効になりません。

もう1つ興味深いのは、このgeneric.xamlには2つの辞書があります。 App.xamlからResourceDictionaryを削除すると、GridComboBox.xamlは正常に動作しますが、DataGridEx.xamlは正しく動作しません。

答えて

3

あなたはこのようApp.xamlにマージする必要があります。

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Generic.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

あなたはすでにそれをやっている場合は、あなたがより多くの情報を与えるためにあなたのコードをポストする必要があります。

編集: hereの指示に従ってください。特に、ディクショナリをリソースに設定し、ソースでフルパスを使用する必要がある部分。

+1

なぜ明示的にマージする必要がありますか?それがテーマフォルダに置かれると、自動的にマージされませんか? – newman

+0

ThemeInfoをあなたの提案に変更しましたが、何の違いもありません。私はいくつかのことを試し、さまざまな問題にぶつかりました。私は私の質問に自分のコードを投稿します。 – newman

+0

ちなみに、別のテストで見つけたように、generic.xamlのビルドアクションをResourceに設定すると動作しませんが、ビルドアクションがデフォルトの "ページ"に設定されていればうまく動作します。 – newman

0

この問題が発生しました。これは、ResourceDictionaryで使用されるスタイルのx:Keyプロパティを削除するのが最も簡単な解決方法です。

<ControlTemplate TargetType="{x:Type Button}"> 
関連する問題