2015-04-24 168 views
0

我使用DataGrid並希望爲單元格引入樣式觸發器,該單元格根據列的IsReadOnly屬性設置單元格的背景顏色。我認爲有一些方法可以使用RelativeSource(一直導航到網格並從那裏回到列)從單元格級別到列級別,但我無法弄清楚使用的確切路徑。WPF:綁定到父元素的屬性

+2

也許嘗試{結合(DataGridTemplateColumn.IsReadOnly)的RelativeSource = {的RelativeSource FindAncestor,AncestorType = {X:類型DataGridTemplateColumn}}}?不太確定,因爲我不知道你的XAML結構。也許更好地發佈你到目前爲止? – Ben

回答

2

將下面的代碼你的DataGrid中:

<DataGrid.CellStyle> 
    <Style TargetType="DataGridCell"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding Path=IsReadOnly, RelativeSource={RelativeSource Self} }" 
          Value="True"> 
       <Setter Property="Background" Value="Red"/> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
</DataGrid.CellStyle>