2016-08-15 62 views
0

當數據從數據庫中獲取時,我想在我的數據網格中添加備用空行。 (雖然AlternationCount="2" AlternatingRowBackground="LightGray"使用,它確實有助於在視覺上偏析行與行,它會更好,如果行與行的數據,就會有空白行,從而具有更大的視覺體驗)C#WPF - 數據網格中的備用空行

感謝

回答

2

我不是一個好主意,通過添加一些廢物數據來改變視覺效果。可能你只需要修改你的datagrid行的樣式,添加一些額外的空間,你可以隨意填寫。

例如,添加新的樣式,你的資源:

<Style x:Key="MyDataGridRowStyle" TargetType="{x:Type DataGridRow}"> 
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> 
    <Setter Property="SnapsToDevicePixels" Value="true"/> 
    <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/> 
    <Setter Property="ValidationErrorTemplate"> 
     <Setter.Value> 
      <ControlTemplate> 
       <TextBlock Foreground="Red" Margin="2,0,0,0" Text="!" VerticalAlignment="Center"/> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <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="*"/> 
         </SelectiveScrollingGrid.ColumnDefinitions> 
         <SelectiveScrollingGrid.RowDefinitions> 
          <RowDefinition Height="*"/> 
          <RowDefinition Height="Auto"/> 
          <RowDefinition Name="ExtraRow" Height="20"/> 
         </SelectiveScrollingGrid.RowDefinitions> 
         <DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
         <DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" 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="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/> 
         <TextBlock Grid.Row="2" Grid.Column="1" Text=" *** Extra space *** " FontSize="8" VerticalAlignment="Center" Margin="100,0"/> 
        </SelectiveScrollingGrid> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="IsNewItem" Value="True"> 
      <Setter Property="Margin" Value="{Binding NewItemMargin, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

然後在你的DataGrid中使用它:

<DataGrid RowStyle="{StaticResource MyDataGridRowStyle}"> 

你會得到這樣的:

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="*"/> 
      </SelectiveScrollingGrid.ColumnDefinitions> 
      <SelectiveScrollingGrid.RowDefinitions> 
       <RowDefinition Name="ExtraRow1" Height="10"/> 
       <RowDefinition Height="*"/> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Name="ExtraRow2" Height="10"/> 
      </SelectiveScrollingGrid.RowDefinitions> 
      <Border Grid.Row="0" Grid.Column="1" BorderThickness="0,0,0,1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" BorderBrush="Black"/> 
      <DataGridCellsPresenter Grid.Column="1" Grid.Row="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="4" 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> 

看起來像

enter image description here

+0

感謝。代碼工作幾乎完成了我。我在IsNewItem上遇到錯誤(在 .....中)。我所做的只是刪除那段代碼,並且它工作得很好(不知道刪除代碼有什麼缺點)。此外,是否有可能在行的頂部有一條線(爲了使行看起來是正方形,覆蓋了4個方向)? – user6648485

+0

IsNewItem屬性起源於.Net 4.5。因此,如果您使用較低版本,則應該爲您的.Net版本獲取DataGridRow的原始模板並對其進行修改。 在WPF中,可以控制幾乎任何您可以想象的視覺外觀 - 只需模板即可。我已經修改了我的答案以滿足您的要求,但不確定我是否完全抓住了您的要求。 – Sergey

+0

@謝爾蓋,看起來很酷,這是我試圖建立(但我還沒有獨立建立)。非常感謝您的指導。 :-) – user6648485

-1

如果你是綁定一個集合到你的數據網格,那麼它將不可能,因爲datagrid是通過查看它的項目來源來填充的。我有一個解決方案,但我不認爲這將不勝感激。

在您的集合中,在每個交替索引處輸入一個空對象。它將在datagrid中創建一個空行。