2017-09-26 20 views
0

私はWPFアプリケーションでDataGridコントロールを持っていて、チェックボックス列をその列の1つとして持っています。私はDataGridTemplateを使ってデータグリッドを構築しています。私の目標は、チェックボックスが無効になっている任意のチェックボックスデータグリッドセルの背景色を変更することです。私のチェックボックスの列は次のようになります:DataGridTemplateColumn無効なコントロールのスタイル設定WPF

<!-- style for the checkbox column cells --> 
<Style x:Key="DefaultCheckboxCellStyle" TargetType="DataGridCell"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type DataGridCell}"> 
       <Grid Background="{TemplateBinding Background}"> 
        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="IsEnabled" Value="True"> 
      <Setter Property="Background" Value="HotPink" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

... 
<DataGridTemplateColumn Header="MyCheckBoxColumn" CellStyle="{StaticResource DefaultCheckboxCellStyle}"> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <CheckBox Name="chk" IsChecked="{Binding MyChkChecked, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding Path=MyChkEnabled}" /> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 
... 

無効なチェックボックスのセルの背景が変更されているようです。無効なチェックボックスを表示していますが、セル自体はすべて有効です。有効な値を内側のチェックボックスコントロールからセルにバインドする方法はありますか?もしそうなら、私はそれを私の引き金に使うことができます。

多くの感謝!

答えて

1

チェックボックスを無効にすると、検出されたとおりにセルが無効になりません。これについては、いくつかの方法があります。

1つは、チェックボックスを無効にする同じviewmodelプロパティにStyleトリガーバインドを設定することです。これはDefaultCheckboxCellStyleに属し、1つのトリガーを置き換えます。

<Style.Triggers> 
    <DataTriger Binding="{Binding MyChkEnabled}" Value="True"> 
     <Setter Property="Background" Value="HotPink" /> 
    </DataTriger> 
</Style.Triggers> 

それともDefaultCheckboxCellStyleでこれを試みることができる:あなたが持っているトリガーを保ち、実際のセルを無効にします。とにかくセルの子であるため、チェックボックス自体を無効にする他のXAMLを省略することができます。

<Setter Property="IsEnabled" Value="{Binding MyChkEnabled}" /> 
<Style.Triggers> 
    <Trigger Property="IsEnabled" Value="True"> 
     <Setter Property="Background" Value="HotPink" /> 
    </Trigger> 
</Style.Triggers> 

UpdateSourceTrigger=PropertyChangedがあります。

+1

これはトリック@Ed Plunkettありがとう! –

関連する問題