2010-12-27 13 views
38

चुना है, मैं एक बहुत ही बेवकूफ समस्या से फंस गया हूं - WPF DataGrid में चयनित पंक्ति को स्टाइल करने की आवश्यकता है।डब्ल्यूपीएफ डाटाग्रिड ने पंक्ति शैली

मैं कुछ रंग के साथ पूरी पंक्ति भरने के बजाय नीली सीमा के साथ एक आयताकार दिखाना चाहता हूं।

कोई विचार यह कैसे कार्यान्वित किया जाए? यह बहुत आसान बनाने के लिए बस कुछ रास्ता होना चाहिए।

उत्तर

78

CellStyle और RowStyleDataGrid पर RowStyle का उपयोग करें। DataGridCell और DataGridRow दोनों में IsSelected संपत्ति है जिसका उपयोग Trigger में किया जा सकता है ताकि यह पता चल सके कि उन्हें चुना गया है या नहीं।

कुछ चाल करना चाहिए निम्नलिखित की तरह:

<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> 

बस के आसपास खेलते हैं जब तक आप इसे सही मिलता है।

+1

सीमा सिर्फ प्रतिपादन है मैं कैसे चाहता था, लेकिन जब एक पंक्ति यह का चयन मैं पृष्ठभूमि/अग्रभूमि रंग, साथ कुछ नहीं कर सकते एक ठोस नीले रंग के साथ भर रहा है –

+0

मुझे समझ में नहीं आता है। क्या आप यह कहने की कोशिश कर रहे हैं कि 'पृष्ठभूमि' और 'अग्रभूमि' रंग लागू नहीं होते हैं? क्या आप उस कोड को पोस्ट कर सकते हैं जिसका आपने उपयोग किया था? आप किस डब्ल्यूपीएफ का उपयोग कर रहे हैं? – decyclone

+0

क्षमा करें, सब कुछ ठीक काम करता है, धन्यवाद) –

5

कोशिश इस

<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

मैंने इस समाधान की कोशिश की। यह काम करता हैं। लेकिन फिर यह एक और समस्या पेश की। मेरे पास एक पंक्ति में एक हाइपर लिंक सेल है। अगर मैं इस सेटटर को IsSelected = true पर लागू करता हूं। तो मुझे दो बार हाइपर लिंक पर क्लिक करना होगा। क्या इससे बचने का कोई तरीका है? – deathrace

5

मुझे पसंद है यह एक:

<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> 
संबंधित मुद्दे