2017-03-24 25 views
0

我需要移動到Windows窗體應用程序的wpf。我有一些問題使相同風格的DataGridView Wpf DataGrid。需要幫助來管理wpf DataGrid中的選定行風格,具體取決於綁定屬性

DataGridView通過DataSource屬性填充List。

public class GameItem 
{ 
    public string Name { get; set; } 
    public bool Selected { get; set; } 
} 

當Selected = True時,DGV上的整行是Red,否則是GreenYellow。 SelectionColor總是與標準顏色相同,因此在行之間移動不會改變任何顏色。

這是DefaultCellStyle:

dataGridViewCellStyle4.BackColor = System.Drawing.Color.GreenYellow; 
dataGridViewCellStyle4.ForeColor = System.Drawing.SystemColors.ControlText; 
dataGridViewCellStyle4.SelectionBackColor = System.Drawing.Color.GreenYellow; 
dataGridViewCellStyle4.SelectionForeColor = System.Drawing.SystemColors.ControlText; 


void dgvDetail_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
{ 
    if (e.RowIndex >= 0) 
    { 
     var gameItem = (sender as DataGridView).Rows[e.RowIndex].DataBoundItem as GameItem; 
     if (gameItem != null && gameItem.Selected) 
     { 
      e.CellStyle.SelectionForeColor = Color.White; 
      e.CellStyle.SelectionBackColor = Color.Red; 
      e.CellStyle.ForeColor = Color.White; 
      e.CellStyle.BackColor = Color.Red; 
     } 
    } 
} 

這是電網在WPF應用程序定義:

<Window x:Class="TestGrid.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:TestGrid" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/> 

     <Style TargetType="DataGrid"> 
      <Style.Resources> 
       <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red"/> 
      </Style.Resources> 
      <Setter Property="FontSize" Value="10pt" ></Setter> 
      <Setter Property="FontFamily" Value="Microsoft Sans Serif" ></Setter> 
      <Setter Property="FontWeight" Value="Bold" ></Setter> 
      <Setter Property="SelectionUnit" Value="FullRow" ></Setter> 
      <Setter Property="HeadersVisibility" Value="Row" ></Setter> 
      <Setter Property="AutoGenerateColumns" Value="False" ></Setter> 
     </Style> 
    </Window.Resources> 
    <Grid> 
     <DataGrid Name="dg1"> 
      <DataGrid.RowStyle> 
       <Style TargetType="{x:Type DataGridRow}"> 
        <Setter Property="Background" Value="GreenYellow"></Setter> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding Selected}" Value="True"> 
          <Setter Property="Background" Value="Red"/> 
          <Setter Property="Foreground" Value="White"/> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </DataGrid.RowStyle> 
      <DataGrid.Columns> 
       <DataGridTextColumn Header="Description" Binding="{Binding Description}" Width="Auto" /> 
       <DataGridTextColumn Header="Manufacturer" Binding="{Binding Manufacturer}" Width="*" /> 
      </DataGrid.Columns> 
     </DataGrid> 
    </Grid> 
</Window> 

我這樣的問題:在WPF我不明白如何顯示

行指示符箭頭到行標題中。

在wpf中,當用戶點擊一行變成選中並改變顏色時,我不想要這個行爲。我只想在有界的Selected item Property爲true時,一行是紅色的。所以點擊一行不能改變顏色,backcolor必須保持綠色,前景白色在數據源中爲Selected = false。

這是Windows窗體版本:

DataGridView

這是WPF中,行1和3正確drawed導致綁定的項目已選擇=真。問題是,當單擊一行(圖片中的1943年)時,即使Selected被選爲false,我也想避免活動行變成像Windows窗體版本一樣突出顯示。

wpf DataGrid

回答

0

在鼠標選擇禁用紅色高亮除去HighlightBrushKey資源(或至少改變thier顏色看出差別)

改變行標題創建一個樣式用於DataGridRowHeader類型:

<Style TargetType="{x:Type DataGridRowHeader}" BasedOn="{StaticResource {x:Type DataGridRowHeader}}">    
    <Setter Property="BorderBrush" Value="Black"/> 
    <Setter Property="BorderThickness" Value="0,0,1,1"/> 
    <Setter Property="Background" Value="GreenYellow"/> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding Selected}" Value="True"> 
      <Setter Property="Background" Value="Red"/>      
     </DataTrigger> 
     <Trigger Property="IsRowSelected" Value="True"> 
      <Setter Property="Content" Value="&#9656;"/>     
      <Setter Property="Background" Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

當Selected屬性爲true時,行標題爲紅色,否則爲GreenYellow。當使用輸入選擇行時,它顯示黑色右指向小三角形字符。

,或者甚至隱藏行標題,並使用DataGridTemplateColumn,而不是在選擇時細胞會顯示>


修改的行和單元格樣式更改選擇前景

<DataGrid.RowStyle> 
    <Style TargetType="{x:Type DataGridRow}"> 
     <Setter Property="Background" Value="GreenYellow"/> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding Selected}" Value="True"> 
       <Setter Property="Background" Value="Red"/> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
</DataGrid.RowStyle> 
<DataGrid.CellStyle> 
    <Style TargetType="DataGridCell"> 
     <Setter Property="Foreground" Value="Black"/> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding Selected}" Value="True"> 
       <Setter Property="Foreground" Value="White"/> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
</DataGrid.CellStyle> 
+0

一切工作正常,除了當一個綠色行成爲selectedRow文本從黑色變爲白色,我希望當通過鼠標選擇一行時文本不會改變。所以我添加了這個。現在綠色的行很好,但是當紅色的bacame將文本更改爲黑色時,即使DataGridRow樣式中的DataTrigger設置了文本Foreground-> white – FDB

+1

@FDB,請參閱我的編輯 – ASh