Quantcast
Channel: Extended WPF Toolkit™ Community Edition
Viewing all articles
Browse latest Browse all 4964

New Post: Applying Styles to each column in Grid Dynamically

$
0
0
Hi,

Here's the sample for option 2), using a ViewModel by column.
<Window x:Class="WpfApplication75.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        xmlns:local="clr-namespace:WpfApplication75"
        Title="MainWindow" Height="350" Width="1000">
   <Window.Resources>
      <local:MyViewModel x:Key="View1"
                         Foreground="Green"/>
      <local:MyViewModel x:Key="View2"
                         Foreground="Green" />
   </Window.Resources>
    
   <Grid>
      <StackPanel>
         <xcdg:DataGridControl x:Name="ResultGrid"
                               AutoCreateColumns="False">
            <xcdg:DataGridControl.Resources>
               <DataTemplate x:Key="test1Template">
                  <TextBlock Text="{Binding}"
                             Foreground="{Binding Foreground, Source={StaticResource View1}}"/>
               </DataTemplate>
               <DataTemplate x:Key="test2Template">
                  <TextBlock Text="{Binding}"
                             Foreground="{Binding Foreground, Source={StaticResource View2}}" />
               </DataTemplate>               
            </xcdg:DataGridControl.Resources>

            <xcdg:DataGridControl.Columns>
               <xcdg:Column Title="id"
                            FieldName="ID">
               </xcdg:Column>

               <xcdg:Column Title="name"
                            FieldName="FirstName"
                            IsMainColumn="True"
                            CellContentTemplate="{StaticResource test1Template}" />
               <xcdg:Column Title="last"
                            FieldName="LastName"
                            CellContentTemplate="{StaticResource test2Template}" />
            </xcdg:DataGridControl.Columns>

         </xcdg:DataGridControl>
         <Button Content="Click Me"
                 Click="Button_Click" />
      </StackPanel>
   </Grid>
</Window>

namespace WpfApplication75
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      this.Results = new ObservableCollection<TestCaseResult>() 
      {
        new TestCaseResult() { ID = 1, FirstName = "AA", LastName = "AAAA" },
        new TestCaseResult() { ID = 1, FirstName = "BB", LastName = "BBBB" },
        new TestCaseResult() { ID = 1, FirstName = "CC", LastName = "CCCC" },
        new TestCaseResult() { ID = 1, FirstName = "DD", LastName = "DDDD"},
        new TestCaseResult() { ID = 1, FirstName = "EE", LastName = "EEEE" },
        new TestCaseResult() { ID = 1, FirstName = "FF", LastName = "FFFF" },
      };
      DataGridCollectionView collectionView = new DataGridCollectionView( this.Results );
      ResultGrid.ItemsSource = collectionView;
    }

    public ObservableCollection<TestCaseResult> Results
    {
      get;
      set;
    }

    private void Button_Click( object sender, RoutedEventArgs e )
    {
      MyViewModel viewModel = this.Resources[ "View1" ] as MyViewModel;
      if( viewModel != null )
      {
        viewModel.Foreground = new SolidColorBrush(Colors.Yellow);
      }
      MyViewModel viewModel2 = this.Resources[ "View2" ] as MyViewModel;
      if( viewModel2 != null )
      {
        viewModel2.Foreground = new SolidColorBrush( Colors.Pink );
      }
    }
  }

  public class TestCaseResult
  {
    public int ID
    {
      get;
      set;
    }

    public string FirstName
    {
      get;
      set;
    }

    public string LastName
    {
      get;
      set;
    }
  }

  public class MyViewModel : DependencyObject
  {
    public Brush Foreground
    {
      get
      {
        return ( Brush )GetValue( ForegroundProperty );
      }
      set
      {
        SetValue( ForegroundProperty, value );
      }
    }

    public static readonly DependencyProperty ForegroundProperty = DependencyProperty.Register( "Foreground", typeof( Brush ), typeof( MyViewModel ),
                                                                  new UIPropertyMetadata(
                                                                  new SolidColorBrush(Colors.Black) ) );
  }
}

Viewing all articles
Browse latest Browse all 4964

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>