Quantcast
Channel: wpftoolkit Discussions Rss Feed
Viewing all articles
Browse latest Browse all 2157

New Post: XCeed DataGrid for WPF Binding

$
0
0
Hi,

Here's a try with CellContentTemplate. My source is a list of DTRow, where each DTRow contains a list of DTCell, where each DTCell has properties like Background, Foreground and Text.
<Window x:Class="WpfApplication58.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
        Title="MainWindow" Height="350" Width="800">
   <Window.Resources>      
      <DataTemplate x:Key="MyCellContentTemplate">
         <Border Background="{Binding Background}">
            <ContentControl Content="{Binding}"
                            Foreground="{Binding Foreground}" />
         </Border>
      </DataTemplate>
   </Window.Resources>
    <Grid>
      <xcdg:DataGridControl x:Name="_dataGrid">
         <xcdg:DataGridControl.Columns>
            <xcdg:Column FieldName="TheList[0]" 
                         Title="Cell1"
                         CellContentTemplate="{StaticResource MyCellContentTemplate}"/>
            <xcdg:Column FieldName="TheList[1]"
                         Title="Cell2"
                         CellContentTemplate="{StaticResource MyCellContentTemplate}" />
            <xcdg:Column FieldName="TheList[2]"
                         Title="Cell3"
                         CellContentTemplate="{StaticResource MyCellContentTemplate}" />
            <xcdg:Column FieldName="TheList[3]"
                         Title="Cell4"
                         CellContentTemplate="{StaticResource MyCellContentTemplate}" />
            <xcdg:Column FieldName="TheList[4]"
                         Title="Cell5"
                         CellContentTemplate="{StaticResource MyCellContentTemplate}" />
            <xcdg:Column FieldName="TheList[5]"
                         Title="Cell6"
                         CellContentTemplate="{StaticResource MyCellContentTemplate}" />
         </xcdg:DataGridControl.Columns>
      </xcdg:DataGridControl>
   </Grid>
</Window>

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

      List<DTCell> cellsList1 = new List<DTCell>()
      {
        new DTCell() { Background = new SolidColorBrush( Colors.Blue), Foreground = new SolidColorBrush( Colors.Black ), Text = "First"},
        new DTCell() { Background = new SolidColorBrush( Colors.Red), Foreground = new SolidColorBrush( Colors.Gray ), Text = "Second" },
        new DTCell() { Background = new SolidColorBrush( Colors.Green), Foreground = new SolidColorBrush( Colors.Black ), Text = "Third" },
        new DTCell() { Background = new SolidColorBrush( Colors.Blue), Foreground = new SolidColorBrush( Colors.Gray ), Text = "Fourth" },
        new DTCell() { Background = new SolidColorBrush( Colors.Red), Foreground = new SolidColorBrush( Colors.Black ), Text = "Fifth" },
        new DTCell() { Background = new SolidColorBrush( Colors.Green), Foreground = new SolidColorBrush( Colors.Gray ), Text = "Sixth" },
      };
      List<DTCell> cellsList2 = new List<DTCell>()
      {
        new DTCell() { Background = new SolidColorBrush( Colors.Orange), Foreground = new SolidColorBrush( Colors.Green ), Text = "First2"},
        new DTCell() { Background = new SolidColorBrush( Colors.Gray), Foreground = new SolidColorBrush( Colors.Blue ), Text = "Second2" },
        new DTCell() { Background = new SolidColorBrush( Colors.Yellow), Foreground = new SolidColorBrush( Colors.Green ), Text = "Third2" },
        new DTCell() { Background = new SolidColorBrush( Colors.Orange), Foreground = new SolidColorBrush( Colors.Blue ), Text = "Fourth2" },
        new DTCell() { Background = new SolidColorBrush( Colors.Gray), Foreground = new SolidColorBrush( Colors.Green ), Text = "Fifth2" },
        new DTCell() { Background = new SolidColorBrush( Colors.Yellow), Foreground = new SolidColorBrush( Colors.Blue ), Text = "Sixth2" },
      };
      List<DTCell> cellsList3 = new List<DTCell>()
      {
        new DTCell() { Background = new SolidColorBrush( Colors.Pink), Foreground = new SolidColorBrush( Colors.Red ), Text = "First3"},
        new DTCell() { Background = new SolidColorBrush( Colors.LightBlue), Foreground = new SolidColorBrush( Colors.Pink ), Text = "Second3" },
        new DTCell() { Background = new SolidColorBrush( Colors.Violet), Foreground = new SolidColorBrush( Colors.Red ), Text = "Third3" },
        new DTCell() { Background = new SolidColorBrush( Colors.Pink), Foreground = new SolidColorBrush( Colors.Pink ), Text = "Fourth3" },
        new DTCell() { Background = new SolidColorBrush( Colors.LightBlue), Foreground = new SolidColorBrush( Colors.Red ), Text = "Fifth3" },
        new DTCell() { Background = new SolidColorBrush( Colors.Violet), Foreground = new SolidColorBrush( Colors.Pink ), Text = "Sixth3" },
      };

      _dataGrid.ItemsSource = new List<DTRow>()
      {
        new DTRow() { TheList = cellsList1 },
        new DTRow() { TheList = cellsList2 },
        new DTRow() { TheList = cellsList3 }
      };

    }
  }

  public class DTRow
  {
    public List<DTCell> TheList
    {
      get;
      set;
    }

    public DTCell this[ int index ]
    {
      get
      {
        return this.TheList[ index ];
      }
    }
  }

  public class DTCell
  {
    public String Text
    {
      get;
      set;
    }
    public Brush Background
    {
      get;
      set;
    }
    public Brush Foreground
    {
      get;
      set;
    }
    public override string ToString()
    {
      return Text;
    }
  }
Here's the result : http://postimg.org/image/q7a3wolx5/

Viewing all articles
Browse latest Browse all 2157

Latest Images

Trending Articles



Latest Images

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