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

New Post: View a datagrid column's data type

$
0
0
Hi,

Maybe you could use "CellContentTemplateSelector" from Column :
<StackPanel>
      <Button Content="Change ItemsSource"
              Click="Button_Click" />
      <xcdg:DataGridControl x:Name="_datagrid">
         <xcdg:DataGridControl.Resources>
            <DataTemplate x:Key="DateTimeTemplate">
               <TextBox Text="{Binding .}"
                        Background="Green" />
            </DataTemplate>

            <DataTemplate x:Key="DoubleTemplate">
               <TextBox Text="{Binding .}"
                        Background="Red" />
            </DataTemplate>
         </xcdg:DataGridControl.Resources>
         <xcdg:DataGridControl.Columns>
            <xcdg:Column FieldName="StartWorkAt"
                         CellContentTemplateSelector="{x:Static local:CellDataTemplateSelector.Singleton}">
            </xcdg:Column>
         </xcdg:DataGridControl.Columns>
      </xcdg:DataGridControl>
</StackPanel>

 public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      _datagrid.ItemsSource = new List<MyData>()
      {
        new MyData() { LastName = "TEST", StartWorkAt = new DateTime( 1987, 12, 8, 15, 0, 0), TheSecondOne = new DateTime(1989, 12, 8, 16, 0, 0) },
        new MyData() { LastName = "TEST2", StartWorkAt = new DateTime( 1987, 12, 9, 15, 0, 0), TheSecondOne = new DateTime(1989, 12, 8, 16, 0, 0)  },
        new MyData() { LastName = "TEST3", StartWorkAt = new DateTime( 1987, 12, 10, 15, 0, 0), TheSecondOne = new DateTime(1989, 12, 8, 16, 0, 0)  },
        new MyData() { LastName = "TEST4", StartWorkAt = new DateTime( 1987, 12, 11, 15, 0, 0), TheSecondOne = new DateTime(1989, 12, 8, 16, 0, 0)  },
        new MyData() { LastName = "TEST5", StartWorkAt = new DateTime( 1987, 12, 12, 15, 0, 0), TheSecondOne = new DateTime(1989, 12, 8, 16, 0, 0)  },
      };
    }

    private void Button_Click( object sender, RoutedEventArgs e )
    {
      _datagrid.ItemsSource = new List<MyData2>()
      {
        new MyData2() { FirstName = "Tom", StartWorkAt = 10d},
        new MyData2() { FirstName = "Kathy", StartWorkAt = 8d  },
        new MyData2() { FirstName = "Jenny", StartWorkAt = 7d  },
        new MyData2() { FirstName = "Brian", StartWorkAt = 8d },
      };
    }
  }

  public class MyData
  {
    public string LastName
    {
      get;
      set;
    }

    public DateTime StartWorkAt
    {
      get;
      set;
    }

    public DateTime TheSecondOne
    {
      get;
      set;
    }
  }

  public class MyData2
  {
    public string FirstName
    {
      get;
      set;
    }

    public double StartWorkAt
    {
      get;
      set;
    }
  }

  public class CellDataTemplateSelector : DataTemplateSelector
  {
    #region Singleton Property

    public static CellDataTemplateSelector Singleton
    {
      get
      {
        if( _singleton == null )
          _singleton = new CellDataTemplateSelector();

        return _singleton;
      }
    }

    private static CellDataTemplateSelector _singleton;

    #endregion Singleton Property

    #region PUBLIC METHODS

    public override DataTemplate SelectTemplate( object item, DependencyObject container )
    {
      DataGridContext dataGridContext = DataGridControl.GetDataGridContext( container );

      DataGridControl parentDataGrid = ( dataGridContext != null )
        ? dataGridContext.DataGridControl
        : null;

      if( parentDataGrid == null || item == null)
        return base.SelectTemplate( item, container );

      if( item is DateTime )
      {
        // When the grid's view is TableView, we want to right-align the value.
        return parentDataGrid.FindResource( "DateTimeTemplate" ) as DataTemplate;
      }

      return parentDataGrid.FindResource( "DoubleTemplate" ) as DataTemplate;
    }

    #endregion PUBLIC METHODS
  }

Viewing all articles
Browse latest Browse all 4964

Trending Articles



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