2012-02-23 138 views
2

我正在清理App.xaml并将一些样式移入Generic.xaml。但是,这些样式没有任何效果。为什么?这是正确的事情做App.xaml整洁吗?为什么我的Generic.xaml样式不起作用?

EDIT(包括一些源代码):

的目标是再整WPF DataGrid中,使从上到下和从左至右(默认样式不包括行和列标题)中运行的滚动条。

当我把它放在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,它不会投诉,但样式也不会生效。

另一个有趣的事情是,在这个generic.xaml中,我有两个字典。如果我从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

为什么我需要明确地合并?当它放置在Themes文件夹中时它不会自动合并吗? – newman 2012-02-23 13:40:57

+0

我将ThemeInfo更改为您的建议,但它没有任何区别。我尝试了几件事情,遇到了不同的问题。我会在我的问题中发布我的代码。 – newman 2012-02-23 14:57:52

+0

顺便说一句,正如我从其他测试中发现的那样,如果将generic.xaml的构建操作设置为Resource,则不起作用,但如果将构建操作设置为默认的“Page”,则工作正常。 – newman 2012-02-24 01:21:36

0

我遇到了这个问题,最简单的解决方法是删除要在ResourceDictionary中使用的样式的x:Key属性。

<ControlTemplate TargetType="{x:Type Button}">