2014-06-22 65 views
2

我目前在WPF中使用MVVM,我在我的项目中安装了MahappsMetro。我想要更改默认的DataGrid样式,而不会丢失MetroDataGrid样式的所有属性(Style for MahappsMetro中的DataGrid)。WPF - 修改MahappsMetro DataGrid风格

我只是想改变一些触发器为IsMouseOver和IsSelected,我试着这样做:

我的App.xaml

<Style x:Key="TransparentDataGrid" TargetType="{x:Type DataGrid}" BasedOn="{StaticResource MetroDataGrid}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type DataGrid}"> 
        <DataGrid> 
         <DataGrid.RowStyle> 
          <Style TargetType="{x:Type DataGridRow}">  
           <Style.Triggers> 
            <Trigger Property="IsMouseOver" Value="True"> 
             <Setter Property="Background" Value="Transparent" /> 
            </Trigger> 
            <Trigger Property="IsSelected" Value="True"> 
             <Setter Property="Background" Value="Transparent" /> 
             <Setter Property="Foreground" Value="Black" /> 
            </Trigger> 
           </Style.Triggers> 
          </Style> 
         </DataGrid.RowStyle> 
         <DataGrid.CellStyle> 
          <Style TargetType="{x:Type DataGridCell}"> 
           <Style.Triggers> 
            <Trigger Property="DataGridCell.IsSelected" Value="True"> 
             <Setter Property="Background" Value="Transparent" /> 
             <Setter Property="BorderBrush" Value="Transparent" /> 
             <Setter Property="Foreground" Value="Black" /> 
            </Trigger> 
           </Style.Triggers> 
          </Style> 
         </DataGrid.CellStyle> 
        </DataGrid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

定义了这个风格,我结合这个风格,我需要它

Style="{DynamicResource TransparentDataGrid}" 

但我得到异常:

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll 

我也试过:

我创建了MainViewModel.xaml的风格,但我不知道怎么这种风格绑定到其他意见。

回答

1

从现有的样式导出,你需要在基于属性

<Style x:Key="TransparentDataGrid" TargetType="{x:Type DataGrid}" BasedOn="{StaticResource {x:Type MetroDataGrid}}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
     ... 

如果上述方法无效,那么请使用正确的命名空间前缀到MetroDataGridmapp:MetroDataGrid其中MAPP点指定类型为关键的控制到MahappsMetro组件

例如

<Application x:Class="CSharpWPF.App" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     StartupUri="MainWindow.xaml" 
     xmlns:mapp="clr-namespace:MahApps.Metro.Controls"> 
<Application.Resources> 
    <Style x:Key="TransparentDataGrid" TargetType="{x:Type DataGrid}" BasedOn="{StaticResource {x:Type mapp:MetroDataGrid}}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       ... 
</Application.Resources> 

使用上述正确的程序集仅仅是一个例子

+0

{StaticData {}:}}是不可能的,因为在App.xaml中,WPF中不支持MetroDataGrid。 – CampDev

+0

您需要为''标签中的相同名称空间定义名称空间。例如'xmlns:mapp =“clr-namespace:MahApps.Metro.Controls”'并将其改为'{StaticResource {x:Type mapp:MetroDataGrid}} – pushpraj

+0

我在尝试,但'{StaticResource {x:Type mapp: Metro DataGrid}}'这是不可能的。 绑定将是'{StaticResource {x:Type mapp:TypeName = DataGrid}}(因为在mapp之后:必须是Type或TypeName)。但是,对于最后的语法,存在一个问题,“x:type参数无效”。 – CampDev