2016-03-23 1 views
0

私はDataGridを持っており、個々のセルの背景色を変更したいと思います。これは、いくつかは、そのようなグローバルCellStyleに基づくDataGrid CellStyle

<DataGridTextColumn.CellStyle> 
    <Style> 
     <Setter Property="Border.Background" Value="{Binding Converter={StaticResource ImportTableBackgroundColorConverter},ConverterParameter=GotName}" /> 
    </Style> 
</DataGridTextColumn.CellStyle> 

としてXAMLで検索した後に行うことが合理的に簡単ですが、アプリ全体のResourceDictionaryに私も

<Style TargetType="DataGrid" x:Key="GlobalCellStyle"> 

    <!-- Cell style --> 
    <Setter Property="CellStyle"> 
     <Setter.Value> 
      <Style TargetType="DataGridCell"> 

       <!-- Single Click Editing --> 
       <EventSetter Event="PreviewMouseLeftButtonDown" 
         Handler="DataGridCell_PreviewMouseLeftButtonDown" /> 
       <EventSetter Event="KeyDown" Handler="DataGridCell_KeyDown" /> 
       <EventSetter Event="GotFocus" Handler="DataGridCell_GotFocus"/> 

       <!--body content datagrid cell vertical centering--> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type DataGridCell}"> 
          <Grid Background="{TemplateBinding Background}"> 
           <ContentPresenter VerticalAlignment="Center" /> 
          </Grid> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </Setter.Value> 
    </Setter> 
</Style> 

を持ってこれには、コンテンツを中央にして、使用するすべてのデータグリッドのセルを設定し、同じファイルを使用するコードビヘイビアの一部は、セルをシングルクリックで編集モードにします。ローカルに新しいスタイルを指定すると、これが失われます。グローバルに基づいて新しいローカルスタイルを指定しようとすると、例外Can only base on a Style with target type that is base type 'IFrameworkInputElement'が発生します。

グローバルDataGridスタイルの外にグローバルなDataGridCellスタイル自体を持ってきて、同じエラーが発生しました。これにもかかわらずDataGridCell appearing to implement IFrameworkInputElement.

ValueConverterにパラメータを渡して、セルが表示しているフィールドを識別できるようにするため、背景色をグローバルスタイルに移動できません。行全体の背景色を一緒に変更します。私のテーブルの各列宣言にグローバルスタイルをコピーするだけでなく、おそらくコードビハインドもコピーしなければならない場合は、最初と後で両方を維持するのは非常に恐ろしいようです。

誰かが私がスタイル継承を働かせるか、ValueConverterが呼び出されたときに私が入っている列を知ることができるか知っていますか? 、元のメソッドが失敗した理由を、私は前者と後者をキャストすることができますので、DataGridCellは明らかにIFrameworkInputElementを実装していても(理解していない、非常に不思議な

<Style BasedOn="{StaticResource GlobalCellStyle}"> 
    <Setter Property="Border.Background" Value="{Binding Converter={StaticResource ImportTableBackgroundColorConverter},ConverterParameter=GotName}" /> 
</Style> 

答えて

0

、すなわち:

0

あなたはおそらくBASEDONを使用する必要があります)継承されたスタイルが継承元のスタイルと同じResourceDictionaryで定義されている場合、それは機能します。すなわち

<Style TargetType="DataGridCell" x:Key="GlobalCellStyle"> 

    <!-- Your DataGrid Cell style definition goes here --> 
    <!-- Single Click Editing --> 
    <EventSetter Event="PreviewMouseLeftButtonDown" 
         Handler="DataGridCell_PreviewMouseLeftButtonDown" /> 
    <EventSetter Event="KeyDown" Handler="DataGridCell_KeyDown" /> 
    <EventSetter Event="GotFocus" Handler="DataGridCell_GotFocus"/> 

    <!--body content datagrid cell vertical centering--> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type DataGridCell}"> 
       <Grid Background="{TemplateBinding Background}"> 
        <ContentPresenter VerticalAlignment="Center" /> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<conv:ImportTableBackgroundColorConverter x:Key="ImportTableBackgroundColorConverter" /> 

<Style BasedOn="{StaticResource GlobalCellStyle}" TargetType="DataGridCell" x:Key="DOBCellStyle"> 
    <Setter Property="Border.Background" Value="{Binding Converter={StaticResource ImportTableBackgroundColorConverter},ConverterParameter=GotDOB}" /> 
</Style> 

ある時点で他の人にとって役に立ちます。

+0

これが原因で例外が発生しました。それが、私がグローバルにローカルスタイルをベースにしている方法です。しかし、提案に感謝します。 –

+1

セルスタイルのTargetTypeを設定しますか? – user3690202

+0

いいえ、「グローバルなDataGridスタイル以外のグローバルなDataGridCellスタイル自体をグローバルなDataGridスタイルの外に持ってきてみました」と同じことをしていました –

関連する問題