2012-01-06 84 views
0

我有一個datagrid,我已經覆蓋了某些自定義主題的行樣式和控件模板。爲了舉例,我在每一行的上面放了一個矩形(高度:1,填充:紅色)。像左邊的形象在這裏 -不同於它的兄弟姐妹的單個元素

enter image description here

這裏是做這個的控制模板,我只是增加了一個紅色矩形的默認模板。

<ControlTemplate TargetType="{x:Type DataGridRow}"> 
        <Border x:Name="DGR_Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> 
         <SelectiveScrollingGrid> 
          <SelectiveScrollingGrid.ColumnDefinitions> 
           <ColumnDefinition Width="Auto"/> 
           <ColumnDefinition Width="Auto"/> 
           <ColumnDefinition Width="*"/> 
          </SelectiveScrollingGrid.ColumnDefinitions> 
          <SelectiveScrollingGrid.RowDefinitions> 
           <RowDefinition Height="*"/> 
           <RowDefinition Height="Auto"/> 
          </SelectiveScrollingGrid.RowDefinitions> 
          **<Rectangle Grid.Row="0" Fill="Red" Height="1" VerticalAlignment="Top" Grid.ColumnSpan="2"/>** 
          <DataGridCellsPresenter Grid.Row="1" Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
          <DataGridDetailsPresenter Grid.Column="1" Grid.Row="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/> 
          <DataGridRowHeader Grid.RowSpan="3" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/> 
         </SelectiveScrollingGrid> 
        </Border> 
       </ControlTemplate> 

但是說我想讓它看起來像右邊的圖像......第一行沒有紅色的矩形。我會怎麼做呢?

我已經遇到了這個問題幾次,因爲雖然我一直在WPF主題 - 我有與TabItem頭,另一個問題,因爲我需要第一個選項卡看起來不同於其餘。使用CSS或jQuery時,在網站上工作時有專門用於此目的的選擇器。即時通訊編寫一個WPF桌面應用程序,我不知道如何從XAML/C#的角度來看它。有人知道嗎?

回答

2

您可以嘗試在PreviousData使用DataTriggerRelativeSource,如果是null你的第一行。

注:如果你做同樣的事情在風格定位DataGridCellsPreviousData將列工作(如前一小區是上一列),所以相同的觸發將影響整個第一列,而比第一行)

1

只需要像這樣改變BorderThickness。

<Border BorderThickness="0,0,0,1" BorderBrush="Red"/> 
+0

該解決方法不適用於我需要做的事情。我不想讓這個例子過於尷尬,所以我簡化了一下。但是我必須能夠以不同方式對一組元素中的第一個元素進行樣式化。 – 2012-01-06 13:32:09

0

我會使用某種觸發器。這樣的事(還沒有測試它): (編輯:嘗試在DataGrid中的datatrigger)

<DataGrid.RowStyle> 
    <Style TargetType="DataGridRow"> 
     <Style.Triggers> 
      <Trigger Property="Grid.Row" Value="0"> 
       <Setter Property="BorderBrush" Value="Black"/> 
       <Setter Property="BorderThickness" Value="0,0,1,1"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</DataGrid.RowStyle> 
+0

這會將BorderThickness和BorderBrush設置爲每行上的值。由於矩形總是在行模板中的Grid.Row = 0。 – 2012-01-06 13:29:21

+0

已更新的答案。我認爲最好不要使用矩形,而應該關注datagrid行屬性。此代碼應該可以工作,但您需要添加更好的觸發器屬性。理想情況下,Value!=「0」,但當然xaml不會編譯它。 http://stackoverflow.com/a/793966/1074345應該幫助你的那一部分。 – emybob 2012-01-06 13:45:02

+0

更新答案仍然影響每一行。 – 2012-01-06 13:54:45