我需要移動到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窗體版本:
這是WPF中,行1和3正確drawed導致綁定的項目已選擇=真。問題是,當單擊一行(圖片中的1943年)時,即使Selected被選爲false,我也想避免活動行變成像Windows窗體版本一樣突出顯示。
一切工作正常,除了當一個綠色行成爲selectedRow文本從黑色變爲白色,我希望當通過鼠標選擇一行時文本不會改變。所以我添加了這個 。現在綠色的行很好,但是當紅色的bacame將文本更改爲黑色時,即使DataGridRow樣式中的DataTrigger設置了文本Foreground-> white –
FDB
@FDB,請參閱我的編輯 – ASh