locked
Set DataGridRow color during run time from a PopUpBox RRS feed

  • Question

  • Hi friends,

     I want to set the color of a DataGridRow as follows. while right clicking a pop up box should come with all(some) available colors and clicking on some  color the corresponding row get the clicked color. how I can implement this.

    Thanks in advance 

    Iqbal


    itismeiqbal

    Wednesday, June 19, 2013 2:47 PM

Answers

  • Add references to System.Windows.Forms and System.Drawing and try the following:

    <DataGrid x:Name="dGrid1" AutoGenerateColumns="False">
            <DataGrid.Columns>
              <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                  <DataTemplate>
                    <Button Content="Change colour" Click="Button_Click_1" />
                  </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
              </DataGridTemplateColumn>
            </DataGrid.Columns>
          </DataGrid>

        private void Button_Click_1(object sender, RoutedEventArgs e) {
          System.Windows.Forms.ColorDialog dialog = new System.Windows.Forms.ColorDialog();
          if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
            Visual visual = sender as Visual;
            while (visual != null) {
              visual = VisualTreeHelper.GetParent(visual) as Visual;
              if (visual is DataGridRow) {
                DataGridRow row = visual as DataGridRow;
                row.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(dialog.Color.A, dialog.Color.R, dialog.Color.G, dialog.Color.B));
                break;
              }
            }
          }
        }

    • Proposed as answer by Lisa Zhu Thursday, June 20, 2013 9:12 AM
    • Marked as answer by Lisa Zhu Wednesday, June 26, 2013 1:02 PM
    Wednesday, June 19, 2013 3:09 PM