2010-12-27 9 views
38

Çok aptalca bir sorunla uğraştım - WPF DataGrid'te seçili satırı biçimlendirmem gerekiyor.WPF DataGrid seçili satır stili

Tüm satıra biraz renk katmak yerine mavi kenarlıklı bir dikdörtgen göstermek istiyorum.

Bunu nasıl uygulayacağınız hakkında herhangi bir fikrin var mı? Bunu kolayca yapmanın bir yolu olmalı.

cevap

78

DataGrid numaralı telefondan CellStyle ve RowStyle kullanın. DataGridCell ve DataGridRow her ikisi de Trigger içinde seçilip seçilmediklerini öğrenmek için IsSelected özelliğine sahiptir. hile yapmak gerekir aşağıdaki gibi

şey: bunu doğru yapana kadar

<DataGrid.CellStyle> 
    <Style TargetType="DataGridCell"> 
     <Style.Triggers> 
      <Trigger Property="IsSelected" 
         Value="True"> 
       <Setter Property="Background" 
         Value="White" /> 
       <Setter Property="Foreground" 
         Value="Black" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</DataGrid.CellStyle> 
<DataGrid.RowStyle> 
    <Style TargetType="DataGridRow"> 
     <Style.Triggers> 
      <Trigger Property="IsSelected" 
         Value="True"> 
       <Setter Property="BorderBrush" 
         Value="Blue" /> 
       <Setter Property="BorderThickness" 
         Value="2" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</DataGrid.RowStyle> 

Hemen oynayabilir.

+1

Sınır sadece render, ancak bir satırı O seçerken ben arka/ön plan renkleri ile bir şey yapamaz katı bir mavi –

+0

ile dolduruyorum anlamıyorum. 'Arka Plan' ve 'Ön Plan' renklerinin geçerli olmadığını söylemeye mi çalışıyorsunuz? Kullandığın kodu gönderebilir misin? Hangi WPF sürümünü kullanıyorsunuz? – decyclone

+0

Üzgünüz, her şey yolunda gidiyor, teşekkürler) –

5

bu

<DataGrid.Resources> 
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/> 

        <Style TargetType="{x:Type DataGridRow}"> 
         <Setter Property="HeaderStyle"> 
          <Setter.Value> 
           <Style TargetType="{x:Type DataGridRowHeader}"> 
            <Setter Property="Visibility" Value="Collapsed"/> 
            <Setter Property="Width" Value="0"/> 
           </Style> 
          </Setter.Value> 
         </Setter> 
         <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" BorderThickness="1" CornerRadius="5" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> 
             <SelectiveScrollingGrid> 
              <SelectiveScrollingGrid.ColumnDefinitions> 
               <ColumnDefinition Width="Auto"/> 
               <ColumnDefinition Width="*"/> 
              </SelectiveScrollingGrid.ColumnDefinitions> 
              <Grid Grid.Column="1"> 
               <Grid.RowDefinitions> 
                <RowDefinition Height="*"/> 
                <RowDefinition Height="Auto"/> 
               </Grid.RowDefinitions> 
               <DataGridCellsPresenter ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
               <DataGridDetailsPresenter Margin="4" 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}"/> 

               <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Grid.RowSpan="2"/> 
              </Grid> 
              <DataGridRowHeader 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.Triggers> 
             <Trigger Property="IsSelected" Value="True"> 
              <Setter TargetName="DGR_Border" Property="BorderBrush" Value="Blue"/> 
             </Trigger> 
            </ControlTemplate.Triggers> 
           </ControlTemplate> 
          </Setter.Value> 
         </Setter> 
         <Style.Triggers> 
          <Trigger Property="DetailsVisibility" Value="Visible"> 
           <Setter Property="BorderThickness" Value="4,1,4,4"/> 
           <Setter Property="BorderBrush" Value="#FF3886B9"/> 
          </Trigger> 
          <!--<Trigger Property="IsSelected" Value="True"> 
           <Setter Property="BorderBrush" Value="Blue"/> 
          </Trigger>--> 
         </Style.Triggers> 
        </Style> 

       </DataGrid.Resources> 
+0

Bu çözümü denedim. İşe yarıyor. ama sonra başka bir problem getirdi. Üst üste bir hiper bağlantılı Hücre var. Bu diziyi IsSelected = true üzerine uygularsam. o zaman iki kere hiper linke tıklaman gerek. Bundan kaçınmak için herhangi bir yol var mı? – deathrace

5

Ben bu bir gibi deneyin: istediğim nasıl

<Style TargetType="{x:Type DataGridRow}"> 
    <Setter Property="BorderBrush" Value="LightGray" /> 
    <Setter Property="BorderThickness" Value="1" /> 
    <Style.Triggers> 
     <Trigger Property="IsSelected" Value="True"> 
      <Setter Property="BorderBrush" Value="Blue" /> 
     </Trigger> 
    </Style.Triggers> 
    <Style.Resources> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" /> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" /> 
    </Style.Resources> 
</Style>