我可以使用样式触发器来为数据网格行着色,但我希望有一个菜单选项可以即时更改颜色。当我单击颜色菜单项并选择我希望在数据网格中反映的样式时。我已阅读有关使用加载行选项的信息,但这需要重新加载数据网格中的所有数据。我试图了解如何使用转换器,但我还没有找到与我正在尝试做的事情完全相关的示例。在我的示例代码中,我只想根据数据网格中的“会议”列更改颜色。
微妙:AFC = 粉色和 NFC = 浅蓝色 鲜艳:AFC = 红色和 NFC = 蓝色
我对 WPF 相当陌生,所以任何建议都值得赞赏。我希望有人可以提供一些可以完成此操作的示例代码。提前致谢。
XAML:
<DockPanel Name="DockPanel1" >:
<Menu Height="30" HorizontalAlignment="Stretch" VerticalAlignment="Top" DockPanel.Dock="Top" >
<MenuItem Header="Color">
<MenuItem Header="Subtle" Click="MenuItem_Click"></MenuItem>
<MenuItem Header="Vivid" Click="MenuItem_Click"></MenuItem>
</MenuItem>
</Menu>
<DataGrid Name="dataGrid1"
AutoGenerateColumns="True"
Width="495" Height="412"
DockPanel.Dock="Top"
ItemsSource="{Binding}">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Conference}" Value="AFC">
<Setter Property="Background" Value="Pink"/>
</DataTrigger>
<DataTrigger Binding="{Binding Conference}" Value="NFC">
<Setter Property="Background" Value="LightBlue" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>
</DockPanel>
代码背后:
namespace DataGridRowColorTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DataTable teams = new DataTable();
public MainWindow()
{
InitializeComponent();
loadDataGrid();
this.DataContext = this;
}
private void loadDataGrid()
{
teams.Columns.Add("Location");
teams.Columns.Add("Name");
teams.Columns.Add("Conference");
teams.Columns.Add("Division");
teams.Rows.Add("Baltimore", "Ravens", "AFC", "North");
teams.Rows.Add("Cincinnati", "Bengals", "AFC", "North");
teams.Rows.Add("Cleveland", "Browns", "AFC", "North");
teams.Rows.Add("Pittsburgh", "Steelers", "AFC", "North");
teams.Rows.Add("Houston", "Texans", "AFC", "South");
teams.Rows.Add("Indianapolis", "Colts", "AFC", "South");
teams.Rows.Add("Jacksonville", "Jaguars", "AFC", "South");
teams.Rows.Add("Tennessee", "Titans", "AFC", "South");
teams.Rows.Add("Buffalo", "Bills", "AFC", "East");
teams.Rows.Add("Miami", "Dolphins", "AFC", "East");
teams.Rows.Add("New England", "Patriots", "AFC", "East");
teams.Rows.Add("New York", "Jets", "AFC", "East");
teams.Rows.Add("Denver", "Broncos", "AFC", "West");
teams.Rows.Add("Kansas City", "Chiefs", "AFC", "West");
teams.Rows.Add("Oakland", "Raiders", "AFC", "West");
teams.Rows.Add("San Diego", "Chargers", "AFC", "West");
teams.Rows.Add("Chicago", "Bears", "NFC", "North");
teams.Rows.Add("Detroit", "Lions", "NFC", "North");
teams.Rows.Add("Green Bay", "Packers", "NFC", "North");
teams.Rows.Add("Minnesota", "Vikings", "NFC", "North");
teams.Rows.Add("Atlanta", "Falcons", "NFC", "South");
teams.Rows.Add("Carolina", "Panthers", "NFC", "South");
teams.Rows.Add("New Orleans", "Saints", "NFC", "South");
teams.Rows.Add("Tampa Bay", "Buccaneers", "NFC", "South");
teams.Rows.Add("Dallas", "Cowboys", "NFC", "East");
teams.Rows.Add("New York", "Giants", "NFC", "East");
teams.Rows.Add("Philadelphia", "Eagles", "NFC", "East");
teams.Rows.Add("Washington", "Redskins", "NFC", "East");
teams.Rows.Add("Arizona", "Cardinals", "NFC", "West");
teams.Rows.Add("San Francisco", "49ers", "NFC", "West");
teams.Rows.Add("Seattle", "Seahawks", "NFC", "West");
teams.Rows.Add("St. Louis", "Rams", "NFC", "West");
dataGrid1.DataContext = teams;
}
}
}