2013-02-12 70 views
2

我有DataGrid以下代碼。如何更改wpf DataGrid中選定的行背景圖像

在alter native row background中有我自己的圖像。所以有可能,那麼我也想改變選定的行上的行背景圖像。

<DataGrid x:Name="dtstandardview" BorderThickness="0" Height="429" Width="688" BorderBrush="Aqua" MouseLeftButtonDown="dtstandardview_MouseRightButtonUp_1" 
        GridLinesVisibility="None" MouseRightButtonUp="dtstandardview_MouseRightButtonUp_1" 
        VerticalScrollBarVisibility="Visible" AutoGenerateColumns="False" IsReadOnly="True" 
         CanUserDeleteRows="False" AlternationCount="2" CanUserResizeRows="False" Sorting="dtstandardview_Sorting_1" 
        Background="#DCDCDC" HeadersVisibility="Column" CanUserResizeColumns="False" 
         RowHeight="27" SelectionUnit="FullRow" CanUserAddRows="False" MinRowHeight="27" LoadingRow="dtstandardview_LoadingRow" LoadingRowDetails="dtstandardview_LoadingRowDetails" Loaded="dtstandardview_Loaded" Initialized="dtstandardview_Initialized" CellEditEnding="dtstandardview_CellEditEnding" AutoGeneratingColumn="dtstandardview_AutoGeneratingColumn" UnloadingRow="dtstandardview_UnloadingRow" UnloadingRowDetails="dtstandardview_UnloadingRowDetails" > 

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

       <DataGrid.CellStyle> 
        <Style TargetType="{x:Type DataGridCell}"> 
         <Setter Property="Background" Value="Transparent"/> 
         <Setter Property="FontSize" Value="12"/> 
         <Setter Property="FontFamily" Value="Arial"/> 
         <Setter Property="Foreground" Value="#404040"/> 
         <Setter Property="BorderBrush" Value="Transparent"/> 
         <Setter Property="BorderThickness" Value="0"/> 


         <Setter Property="Effect"> 
          <Setter.Value> 
           <DropShadowEffect BlurRadius="0" Color="#FF000000" Direction="-60" Opacity="0.32" ShadowDepth="1"/> 
          </Setter.Value> 
         </Setter> 
         <Setter Property="Margin" Value="10,5" /> 
         <Setter Property="VerticalContentAlignment" Value="Bottom"/> 
         <Style.Triggers> 
          <Trigger Property="IsSelected" Value="True"> 
           <Setter Property="Background" Value="#cde0e5"/> 
           <Setter Property="FontSize" Value="12"/> 
           <Setter Property="FontFamily" Value="Arial"/> 
           <Setter Property="Foreground" Value="#404040"/> 
           <Setter Property="BorderBrush" Value="Transparent"/> 
           <Setter Property="BorderThickness" Value="0"/> 
           <Setter Property="Effect"> 
            <Setter.Value> 
             <DropShadowEffect BlurRadius="0" Color="#FF000000" Direction="-60" Opacity="0.32" ShadowDepth="1"/> 
            </Setter.Value> 
           </Setter> 
           <Setter Property="Margin" Value="10,5" /> 
           <Setter Property="VerticalContentAlignment" Value="Bottom"/> 
          </Trigger> 
         </Style.Triggers> 

        </Style> 
       </DataGrid.CellStyle> 
       <DataGrid.RowStyle> 
        <Style TargetType="{x:Type DataGridRow}"> 
         <Setter Property="FontSize" Value="12"/> 
         <Setter Property="FontFamily" Value="Arial"/> 
         <Setter Property="Foreground" Value="#404040"/> 
         <Setter Property="Background" Value="Transparent"/> 
         <Setter Property="DataContext"> 
          <Setter.Value> 
           <TextBlock Margin="10,0,0,0" TextWrapping="Wrap" Text="{Binding}"> 
            <TextBlock.Effect> 
             <DropShadowEffect BlurRadius="0" Color="#FF000000" Direction="-60" Opacity="0.32" ShadowDepth="1"/> 
            </TextBlock.Effect> 
           </TextBlock> 
          </Setter.Value> 
         </Setter> 
         <Style.Triggers> 
          <Trigger Property="IsSelected" Value="True"> 
           <Setter Property="Background" > 
            <Setter.Value> 
             <ImageBrush ImageSource="/ClientApplication;component/Images/bonus_progress_bg.png"/> 

            </Setter.Value> 
           </Setter> 
          </Trigger> 
         </Style.Triggers> 
        </Style> 
       </DataGrid.RowStyle> 
       <DataGrid.RowBackground > 
        <ImageBrush ImageSource="/ClientApplication;component/Images/second_row_bg.png"/> 
       </DataGrid.RowBackground> 
       <DataGrid.AlternatingRowBackground> 
        <ImageBrush ImageSource="/ClientApplication;component/Images/bonus_progress_bg.png"/> 
       </DataGrid.AlternatingRowBackground> 



       <DataGrid.ColumnHeaderStyle> 
        <Style TargetType="{x:Type DataGridColumnHeader}"> 
         <Setter Property="VerticalContentAlignment" Value="Center" /> 
         <Setter Property="ContentTemplate" > 
          <Setter.Value> 
           <DataTemplate> 
            <TextBlock Foreground="#404040" FontWeight="Bold" Margin="10,0,0,0" TextWrapping="Wrap" Text="{Binding}" TextOptions.TextFormattingMode="Display"> 
             <TextBlock.Effect> 
              <DropShadowEffect BlurRadius="0" Color="#FFFFFF" Direction="-90" Opacity="0.40" ShadowDepth="1" RenderOptions.ClearTypeHint="Auto" /> 
             </TextBlock.Effect> 
            </TextBlock> 

           </DataTemplate> 
          </Setter.Value> 
         </Setter> 

         <Setter Property="Background"> 
          <Setter.Value> 
           <ImageBrush ImageSource="/ClientApplication;component/Images/table_bg_header.png"/> 
          </Setter.Value> 
         </Setter> 
         <Setter Property="BorderBrush"> 
          <Setter.Value> 
           <ImageBrush ImageSource="/ClientApplication;component/Images/titel_bg.png"/> 
          </Setter.Value> 
         </Setter> 

         <Setter Property="Foreground" Value="#404040" /> 
         <Setter Property="BorderThickness" Value="0, 0, 1, 0"/> 
         <Setter Property="Height" Value="26" /> 
         <Setter Property="FontSize" Value="14"/> 
         <Setter Property="FontFamily" Value="Arial"/> 

        </Style> 
       </DataGrid.ColumnHeaderStyle> 

      </DataGrid> 

當我選擇我在網格上獲得以下影響的行。

enter image description here

我想用淺藍色整個行。

回答

0

您可以使用使用TemplateBinding

<Border Name="DataGridCellBorder" 
     Background="{TemplateBinding Background}"/> 
+0

感謝您的快速回復。你能解釋我在哪裏必須這樣做嗎? – ujjaval 2013-02-12 07:07:38

+0

http://msdn.microsoft.com/en-us/library/ms742882.aspx – 2013-02-12 07:09:54

0

以前我都面臨着同樣的問題,所以我也跟着下面的方法,它是爲我工作得很好。一旦嘗試...

您正在指定一些寬度到網格頂部行右(您正在使用按鈕)?只要給綁定元素賦予相同的寬度即如果你正在使用文本塊給出的寬度與其頭部相同。

+0

雖然這可能會回答這個問題,爲了改善您的答案,請提供一個代碼示例,以便OP可以與之相關聯。 – davejal 2015-11-24 15:13:00