WPF:从菜单选项更改数据网格行的颜色



我可以使用样式触发器来为数据网格行着色,但我希望有一个菜单选项可以动态更改颜色。 当我单击颜色菜单项并选择我希望反映在数据网格中的样式时。 我已经阅读了有关使用加载行选项的信息,但这需要重新加载数据网格中的所有数据。 我试图了解如何使用转换器,但我还没有找到与我正在尝试做的事情完全相关的示例。 在我的示例代码中,我只想根据数据网格中的"会议"列更改颜色。

微妙: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;
    }

}

}

如果你想随心所欲地使用转换器,你应该做这样的事情:XAML

   <Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Conv="clr-namespace:WpfApplication1"
    Title="MainWindow"
    Width="525"
    Height="350">
<Window.Resources>
    <Conv:ConverterColor x:Key="convColor"></Conv:ConverterColor>
</Window.Resources>
<DockPanel Name="DockPanel1">
    <Menu Height="30"
          HorizontalAlignment="Stretch"
          VerticalAlignment="Top"
          DockPanel.Dock="Top">
        <MenuItem Header="Color">
            <MenuItem Click="MenuItem_Click"  Header="Subtle" />
            <MenuItem Click="MenuItem_Click" Header="Vivid" />
        </MenuItem>
    </Menu>
    <DataGrid Name="dataGrid1"
              Width="495"
              Height="412"
              AutoGenerateColumns="True"
              DockPanel.Dock="Top"
              ItemsSource="{Binding}">
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Setter Property="Background" Value="{Binding Conference,Converter={StaticResource ResourceKey=convColor}}"></Setter>
                <!--<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 WpfApplication1
{
/// <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;
    }
    private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
    }

}
[ValueConversion(typeof(string), typeof(Brush))]
 public class  ConverterColor:IValueConverter
 {
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
     {
         SolidColorBrush solColBrsh=new SolidColorBrush(Colors.Red); 
         string conference = (value as string);
         switch (conference)
         {
             case "AFC":
                 solColBrsh = new SolidColorBrush(Colors.Pink);
                 break;  
             case "NFC":
                 solColBrsh = new SolidColorBrush(Colors.BlueViolet);
                 break;  
             default:
                 break;
         }
         return solColBrsh; 
     }
     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
     {
         throw new NotImplementedException();
     }
 }

}

希望这个帮助

您可以使用 资源:

<Window.Resources>
    <SolidColorBrush x:Key="afc" Color="Pink" />
    <SolidColorBrush x:Key="nfc" Color="LightBlue" />
</Window.Resources>

并动态引用它们:

<DataTrigger Binding="{Binding Conference}" Value="AFC">
    <Setter Property="Background" Value="{DynamicResource afc}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Conference}" Value="NFC">
    <Setter Property="Background" Value="{DynamicResource nfc}" />
</DataTrigger>

然后,您只需要两个单独的MenuItem.Click处理程序即可更改资源中的画笔:

private void MenuItem1_Click(object sender, RoutedEventArgs e)
{
    this.Resources["afc"] = Brushes.Pink;
    this.Resources["nfc"] = Brushes.LightBlue;
}
private void MenuItem2_Click(object sender, RoutedEventArgs e)
{
    this.Resources["afc"] = Brushes.Red;
    this.Resources["nfc"] = Brushes.Blue;
}

相关内容

  • 没有找到相关文章

最新更新