<Window x:Class="SOTC_DataGridExample.Window1" Name="This"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:SOTC_DataGridExample"
Title="DataGrid Example" Height="300" Width="500">
<Window.Resources>
<ObjectDataProvider x:Key="myEnum" ObjectType="{x:Type sys:Enum}" MethodName="GetValues">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:GenderType"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ObjectDataProvider x:Key="myContinentEnum" ObjectType="{x:Type sys:Enum}" MethodName="GetValues">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:Continent"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid x:Name="dgRoot" DataContext="{Binding}">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<dg:DataGrid x:Name="myGrid" ItemsSource="{Binding}" Grid.Row="0" Grid.Column="0" IsSynchronizedWithCurrentItem="True"
Margin="5" AutoGenerateColumns="False" Width="365">
<dg:DataGrid.Columns>
<dg:DataGridTextColumn Binding="{Binding GameName}" Header="Game Name" />
<dg:DataGridTextColumn Binding="{Binding Creator}" Header="Creator" />
<dg:DataGridComboBoxColumn Header="Gender" ItemsSource="{Binding Source={StaticResource myEnum}}"
SelectedItemBinding="{Binding Gender}" />
<dg:DataGridComboBoxColumn Header="Publisher" x:Name="_PublisherCombo"
SelectedItemBinding="{Binding Publisher}" />
<dg:DataGridCheckBoxColumn Binding="{Binding OnXbox}"
Header="On Xbox 360" />
</dg:DataGrid.Columns>
</dg:DataGrid>
<dg:DataGrid x:Name="CountryGrid" Grid.Row="1" Grid.Column="0" IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Path=CurrentItem.CityInfo}" AutoGenerateColumns="False" Width="195">
<dg:DataGrid.Columns>
<dg:DataGridTextColumn Binding="{Binding City}" Header="City" />
<dg:DataGridTextColumn Binding="{Binding Country}" Header="Country" />
<dg:DataGridComboBoxColumn Header="Continent" ItemsSource="{Binding Source={StaticResource myContinentEnum}}"
SelectedItemBinding="{Binding CountryIn}" />
</dg:DataGrid.Columns>
</dg:DataGrid>
</Grid>
</Window>
Code Behind:
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Collections.ObjectModel;
namespace SOTC_DataGridExample
{
public enum GenderType { Male, Female };
public enum Continent{NorthAmerica,Asia,Europe,Australia};
public partial class Window1 : Window
{
private DataTable _GameData;
public Window1()
{
List<GameData> _GameData = new List<GameData>();
List<CityInfo> _cityInfo = new List<CityInfo>();
_cityInfo.Add(new CityInfo() { City = "Mumbai", Country = "India", CountryIn = Continent.Asia });
_cityInfo.Add(new CityInfo() { City = "London", Country = "UK", CountryIn = Continent.Europe });
_cityInfo.Add(new CityInfo() { City = "Atlanta", Country = "USA", CountryIn = Continent.NorthAmerica });
_cityInfo.Add(new CityInfo() { City = "Sydney", Country = "Australia", CountryIn = Continent.Australia });
List<CityInfo> _cityInfo1 = new List<CityInfo>();
_cityInfo1.Add(new CityInfo() { City = "Delhi", Country = "India", CountryIn = Continent.Asia });
_cityInfo1.Add(new CityInfo() { City = "Leeds", Country = "UK", CountryIn = Continent.Europe });
_cityInfo1.Add(new CityInfo() { City = "New York", Country = "USA", CountryIn = Continent.NorthAmerica });
_cityInfo1.Add(new CityInfo() { City = "Melbourne", Country = "Australia", CountryIn = Continent.Australia });
GameData gamedata;
_GameData.Add(new GameData() {CityInfo=_cityInfo,GameName = "World Of Warcraft", Creator = "Blizzard", Publisher = "Blizzard", OnXbox = false, Gender = GenderType.Male });
_GameData.Add(new GameData() { CityInfo = _cityInfo1, GameName = "Halo 3", Creator = "Bungie", Publisher = "Microsoft", OnXbox = true, Gender = GenderType.Female });
_GameData.Add(new GameData() {CityInfo=_cityInfo, GameName = "Gears Of War", Creator = "Epic", Publisher = "Microsoft", OnXbox = true, Gender = GenderType.Male });
_GameData.Add(new GameData() {CityInfo=_cityInfo, GameName = "World Of Warcraft", Creator = "Blizzard", Publisher = "Blizzard", OnXbox = false, Gender = GenderType.Male });
/*_GameData = new DataTable();
_GameData.Columns.Add(new DataColumn("Game Name", typeof(string)));
_GameData.Columns.Add(new DataColumn("Creator", typeof(string)));
_GameData.Columns.Add(new DataColumn("Publisher", typeof(string)));
_GameData.Columns.Add(new DataColumn("On Xbox", typeof(bool)));
_GameData.Columns.Add(new DataColumn("Gender", typeof(GenderType)));
var row = _GameData.NewRow();
_GameData.Rows.Add(row);
row["Game Name"] = "World Of Warcraft";
row["Creator"] = "Blizzard";
row["Publisher"] = "Blizzard";
row["On Xbox"] = false;
row["Gender"] = GenderType.Male;
row = _GameData.NewRow();
_GameData.Rows.Add(row);
row["Game Name"] = "Halo 3";
row["Creator"] = "Bungie";
row["Publisher"] = "Microsoft";
row["On Xbox"] = true;
row["Gender"] = GenderType.Female;
row = _GameData.NewRow();
_GameData.Rows.Add(row);
row["Game Name"] = "Gears Of War";
row["Creator"] = "Epic";
row["Publisher"] = "Microsoft";
row["On Xbox"] = true;
row["Gender"] = GenderType.Male;
_GameData.RowChanged += GameDataRowChanged;
_GameData.RowDeleted += GameDataRowChanged;*/
InitializeComponent();
_PublisherCombo.ItemsSource = new List<string>() { "Activision", "Ubisoft",
"Microsoft", "Blizzard", "Nintendo", "Electronic Arts",
"Take-Two Interactive" };
dgRoot.DataContext = _GameData;
//myGrid.DataContext = _GameData;
//ItemsSource="{Binding ElementName=This, Path=GameData}"
}
private void GameDataRowChanged(object sender, DataRowChangeEventArgs e)
{
Console.WriteLine("----Row Changed----");
Console.WriteLine("Action: " + e.Action);
if (e.Action != DataRowAction.Delete)
{
Console.Write("Values: ");
foreach (var val in e.Row.ItemArray)
{ Console.Write(val + ", "); }
Console.WriteLine();
}
Console.WriteLine("-------------------");
}
public DataTable GameData
{ get { return _GameData; } }
}
public class GameData
{
public string GameName{get;set;}
public string Creator{get;set;}
public string Publisher{get;set;}
public bool OnXbox{get;set;}
public GenderType Gender{get;set;}
public List<CityInfo> CityInfo { get; set; }
}
public class CityInfo
{
public string Country { get; set; }
public string City { get; set; }
public Continent CountryIn { get; set; }
}
}
No comments:
Post a Comment