2016-02-05 62 views
0

我有一个DataGridWPF,它被设置为ItemSource。我想要的是用给定的索引对DataGridRow着色。到目前为止,我这样做,如何将颜色设置为带索引的WPF Datagrid?

DataGridRow row = (DataGridRow)dgv_RStructure.ItemContainerGenerator.ContainerFromIndex(int.Parse(id)); 
row.Background = (SolidColorBrush)(Converter.ConvertFrom(Color)); 

这段代码唯一的问题是我给什么都索引,它的色彩,指数和电网的几个其他指标。我不知道为什么会发生这种情况,任何人都可以帮忙。

+0

您是否尝试过'XAML'中的行样式?尝试查看[DataGridRowStyle](https://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.rowstyle(v = vs.110).aspx) – CBreeze

+0

它不会在这做这项工作案件。 –

回答

0

我怀疑它与虚拟化有关 - 它重用DataGridRow来改变背景。你可以关闭虚拟化,但如果你的ItemsSource有很多项目,那么性能将会受到影响。将VirtualizingPanel.VirtualizationMode =“Standard”添加到DataGrid以关闭虚拟化。

您也可以使用Style,DataTrigger和Converter(并启用虚拟化)。

XAML:

<Window x:Class="WpfApplication6.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:WpfApplication6" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <local:GridRowConverter x:Key="GridRowConverter" /> 
    </Window.Resources> 
    <Window.DataContext> 
     <local:ViewModel HighlightedIndex="5" /> 
    </Window.DataContext> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 
     <TextBlock Text="Highlighted Index" VerticalAlignment="Center" /> 
     <TextBox Grid.Column="1" Text="{Binding HighlightedIndex, UpdateSourceTrigger=PropertyChanged}" Margin="5" /> 
     <DataGrid Grid.Row="1" Grid.ColumnSpan="2" ItemsSource="{Binding Items}"> 
      <DataGrid.RowStyle> 
       <Style TargetType="DataGridRow"> 
        <Style.Triggers> 
         <DataTrigger Value="true"> 
          <DataTrigger.Binding> 
           <MultiBinding Converter="{StaticResource GridRowConverter}"> 
            <Binding /> <!-- 'ViewData' item for this row --> 
            <Binding RelativeSource="{RelativeSource AncestorType=DataGrid}" Path="ItemsSource" /> <!-- The list of items --> 
            <Binding RelativeSource="{RelativeSource AncestorType=Window}" Path="DataContext.HighlightedIndex" /> <!-- Index of item to highlight --> 
           </MultiBinding> 
          </DataTrigger.Binding> 
          <Setter Property="Background" Value="Red" /> 
          <Setter Property="Foreground" Value="Yellow" /> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </DataGrid.RowStyle> 
     </DataGrid> 
    </Grid> 
</Window> 

代码:

using System; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.Globalization; 
using System.Runtime.CompilerServices; 
using System.Windows; 
using System.Windows.Data; 

namespace WpfApplication6 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 
    } 

    public class ViewData : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 
     protected void OnPropertyChanged([CallerMemberName] string propName = null) 
     { 
      if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propName)); 
     } 

     private string item; 
     public string Item { get { return item; } set { item = value; OnPropertyChanged(); } } 

     public ViewData(string s) 
     { 
      Item = s; 
     } 
    } 
    public class ViewModel : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 
     protected void OnPropertyChanged([CallerMemberName] string propName = null) 
     { 
      if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propName)); 
     } 

     private ObservableCollection<ViewData> items; 
     public ObservableCollection<ViewData> Items { get { return items; } set { items = value; OnPropertyChanged(); } } 

     private int highlightedIndex; 
     public int HighlightedIndex { get { return highlightedIndex; } set { highlightedIndex = value; OnPropertyChanged(); } } 

     public ViewModel() 
     { 
      HighlightedIndex = -1; 
      Items = new ObservableCollection<ViewData>(); 
      for (int i = 0; i < 100; i++) 
       Items.Add(new ViewData("Item " + i)); 
     } 
    } 

    public class GridRowConverter : IMultiValueConverter 
    { 
     public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
     { 
      if (values.Length == 3) 
      { 
       var item = values[0] as ViewData; 
       var items = values[1] as ObservableCollection<ViewData>; 
       var idx = -1; 
       if (values[2] is int) 
        idx = (int)values[2]; 
       if (idx >= 0 
        && items != null 
        && items.Count > idx 
        && items[idx] == item) 
         return true; 
      } 
      return false; 
     } 

     public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
} 

截图:

enter image description here

0

在你的代码试试这个:

 // List of items from the datagrid 
     var itemsSource = dgv_RStructure.ItemsSource as IList; 

     // if such items exist 
     if (itemsSource != null) 
     { 
      // Get each item as a datagrid row based on a selected index. 
      var row = dgv_RStructure.ItemContainerGenerator.ContainerFromItem(itemsSource[int.Parse(id)]) as DataGridRow; 

      // if the selected Datagrid Row exists 
      if (row != null) 
      { 
       // change the row background to the preferred color. 
       row.Background = Your_SolidColorBrush; 
      } 
     } 

下面是一个例子应用 1. XAML

<Window x:Class="Solutions.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="*"></RowDefinition> 
       <RowDefinition Height="80"></RowDefinition> 
       <RowDefinition Height="30"></RowDefinition> 
      </Grid.RowDefinitions> 

      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="600"></ColumnDefinition> 
       <ColumnDefinition Width="*"></ColumnDefinition> 
      </Grid.ColumnDefinitions> 
      <DataGrid x:Name="dgCustomers" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding Path=Customers}"/> 

      <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Vertical"> 
       <ComboBox Height="50" Margin="10" ItemsSource="{Binding Path=Colours}" SelectedIndex="0" SelectionChanged="ComboBox_SelectionChanged"> 
        <ComboBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Orientation="Horizontal" Height="50"> 
           <Border Width="30" Height="30" Background="{Binding}" Margin="10"/> 
           <TextBlock Text="{Binding Path=Color}" VerticalAlignment="Center" Margin="10"/> 
          </StackPanel> 
         </DataTemplate> 
        </ComboBox.ItemTemplate> 
       </ComboBox> 
      </StackPanel> 

      <StackPanel Grid.Row="1" Grid.ColumnSpan="3" Orientation="Horizontal"> 
       <Button Margin="5" Width="50" Tag="0" Click="Button_Click">01</Button> 
       <Button Margin="5" Width="50" Tag="1" Click="Button_Click">02</Button> 
       <Button Margin="5" Width="50" Tag="2" Click="Button_Click">03</Button> 
       <Button Margin="5" Width="50" Tag="3" Click="Button_Click">04</Button> 
       <Button Margin="5" Width="50" Tag="4" Click="Button_Click">05</Button> 
       <Button Margin="5" Width="50" Tag="5" Click="Button_Click">06</Button> 
       <Button Margin="5" Width="50" Tag="6" Click="Button_Click">07</Button> 
       <Button Margin="5" Width="50" Tag="7" Click="Button_Click">08</Button> 
       <Button Margin="5" Width="50" Tag="8" Click="Button_Click">09</Button> 
       <Button Margin="5" Width="50" Tag="9" Click="Button_Click">10</Button> 
      </StackPanel> 
     </Grid> 
    </Window> 
  • CODE:

    using System; 
    using System.Collections; 
    using System.Collections.Generic; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Data; 
    using System.Windows.Documents; 
    using System.Windows.Input; 
    using System.Windows.Media; 
    using System.Windows.Media.Imaging; 
    using System.Windows.Navigation; 
    using System.Windows.Shapes; 
    
    namespace Solutions 
    { 
        /// <summary> 
        /// Interaction logic for MainWindow.xaml 
        /// </summary> 
        public partial class MainWindow : Window 
        { 
         public MainWindow() 
         { 
          this.InitializeComponent(); 
          this.InitializeData(); 
          this.DataContext = this; 
         } 
    
         // Customers Sample Data and Colors 
         private void InitializeData() 
         { 
          // Sample Data: List of Customers 
          this.Customers = new List<Customer> 
          { 
           new Customer(){ CustomerID = 01, Code = "CST-00A-001", Name = "Name", Surname = "Surname", Telephone = "12345567890", Email = "[email protected]"}, 
           new Customer(){ CustomerID = 02, Code = "CST-00A-002", Name = "Name", Surname = "Surname", Telephone = "12345567890", Email = "[email protected]"}, 
           new Customer(){ CustomerID = 03, Code = "CST-00A-003", Name = "Name", Surname = "Surname", Telephone = "12345567890", Email = "[email protected]"}, 
           new Customer(){ CustomerID = 04, Code = "CST-00A-004", Name = "Name", Surname = "Surname", Telephone = "12345567890", Email = "[email protected]"}, 
           new Customer(){ CustomerID = 05, Code = "CST-00A-005", Name = "Name", Surname = "Surname", Telephone = "12345567890", Email = "[email protected]"}, 
           new Customer(){ CustomerID = 06, Code = "CST-00A-006", Name = "Name", Surname = "Surname", Telephone = "12345567890", Email = "[email protected]"}, 
           new Customer(){ CustomerID = 07, Code = "CST-00A-007", Name = "Name", Surname = "Surname", Telephone = "12345567890", Email = "[email protected]"}, 
           new Customer(){ CustomerID = 08, Code = "CST-00A-008", Name = "Name", Surname = "Surname", Telephone = "12345567890", Email = "[email protected]"}, 
           new Customer(){ CustomerID = 09, Code = "CST-00A-009", Name = "Name", Surname = "Surname", Telephone = "12345567890", Email = "[email protected]"}, 
           new Customer(){ CustomerID = 10, Code = "CST-00A-010", Name = "Name", Surname = "Surname", Telephone = "12345567890", Email = "[email protected]"} 
          }; 
    
          // Colors to be used as row backgrounds 
          this.Colours = new List<SolidColorBrush> 
          { 
           new SolidColorBrush(Colors.Red), 
           new SolidColorBrush(Colors.Orange), 
           new SolidColorBrush(Colors.Yellow), 
           new SolidColorBrush(Colors.Green), 
           new SolidColorBrush(Colors.Blue), 
           new SolidColorBrush(Colors.Indigo), 
           new SolidColorBrush(Colors.Violet) 
          };   
         } 
    
    
         // When selected color is changed. 
         private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
         { 
          // Set preferred color from combo-box selection 
          ComboBox cb = sender as ComboBox; 
          this.SelectedColor = cb.SelectedItem as SolidColorBrush; 
         } 
    
         // When a button is clicked 
         private void Button_Click(object sender, RoutedEventArgs e) 
         { 
          // Get the clicked button 
          var btn = sender as Button; 
    
          // use its tag property as a row index 
          int rowIndex = Convert.ToInt32(btn.Tag); 
    
          // Change the selected row background to the preferred color. 
          this.ChangeRowBackground(rowIndex); 
         } 
    
         // Change the selected row background to the preferred color. 
         private void ChangeRowBackground(int selectedIndex) 
         { 
          // List of items from the datagrid 
          var itemsSource = dgCustomers.ItemsSource as IList; 
    
          // if such items exist 
          if (itemsSource != null) 
          { 
           // Get each item as a datagrid row based on a selected index. 
           var row = dgCustomers.ItemContainerGenerator.ContainerFromItem(itemsSource[selectedIndex]) as DataGridRow; 
    
           // if the selected Datagrid Row exists 
           if (row != null) 
           { 
            // change the row background to the preferred color. 
            row.Background = SelectedColor; 
           } 
          } 
         } 
    
         // Public Properties 
         public IList<Customer> Customers { get; set; } 
         public IList<SolidColorBrush> Colours { get; set; } 
         public SolidColorBrush SelectedColor { get; set; } 
        } 
    
        //Customer Class 
        public class Customer 
        { 
         // Properties 
         public int CustomerID { get; set; } 
         public string Code { get; set; } 
         public string Name { get; set; } 
         public string Surname { get; set; } 
         public string Telephone { get; set; } 
         public string Email { get; set; } 
        } 
    } 
    
  • 截屏(1):

  • Select preferred color from the Combo-Box

  • 截屏(2)
  • Use the buttons to Change Row Background to the selected preferred color

  • 截屏(3)
  • Other color can be assigned to different rows.

    祝你好运!

    相关问题