Skip to content

Instantly share code, notes, and snippets.

@jogibear9988
Created July 12, 2013 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jogibear9988/5983986 to your computer and use it in GitHub Desktop.
Save jogibear9988/5983986 to your computer and use it in GitHub Desktop.
# HG changeset patch
# User jkuehner
# Date 1373630899 -7200
# Fri Jul 12 14:08:19 2013 +0200
# Node ID f57b80cbd9298a4e195c080cfb97ffae12951fca
# Parent a9cca0a755c4cc119e4d83f19c8eb1512e88af6a
Support for DataTable
diff -r a9cca0a755c4 -r f57b80cbd929 Source/Examples/ItemsGrid/FeaturesDemo/Examples/ViewModel.cs
--- a/Source/Examples/ItemsGrid/FeaturesDemo/Examples/ViewModel.cs Fri Jan 11 08:35:23 2013 +0100
+++ b/Source/Examples/ItemsGrid/FeaturesDemo/Examples/ViewModel.cs Fri Jul 12 14:08:19 2013 +0200
@@ -27,6 +27,9 @@
// The view model.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
+
+using System.Data;
+
namespace FeaturesDemo
{
using System;
@@ -116,8 +119,20 @@
21 * Mass.Kilogram, 22 * Mass.Kilogram, 23 * Mass.Kilogram,
31 * Mass.Kilogram, 32 * Mass.Kilogram, 33 * Mass.Kilogram
};
+
+
+ DataTable501 = new DataTable();
+ DataTable501.Columns.Add("a");
+ DataTable501.Columns.Add("b");
+ DataTable501.Rows.Add(new object[] {1, 2});
+ DataTable501.Rows.Add(new object[] {3, 4});
+ DataTable501.Rows.Add(new object[] { 5, 6 });
+ DataTable501.Rows.Add(new object[] { 7, 8 });
+ DataTable501.Rows.Add(new object[] { 9, 10 });
}
+ public DataTable DataTable501 { get; set; }
+
/// <summary>
/// Gets or sets the items.
/// </summary>
diff -r a9cca0a755c4 -r f57b80cbd929 Source/Examples/ItemsGrid/FeaturesDemo/Examples/Window501.xaml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Source/Examples/ItemsGrid/FeaturesDemo/Examples/Window501.xaml Fri Jul 12 14:08:19 2013 +0200
@@ -0,0 +1,15 @@
+<Window x:Class="FeaturesDemo.Window501"
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+ xmlns:p="clr-namespace:PropertyTools.Wpf;assembly=PropertyTools.Wpf"
+ xmlns:local="clr-namespace:FeaturesDemo"
+ Title="double[] WrapItems" Height="450" Width="300"
+ FocusManager.FocusedElement="{Binding ElementName=grid1}">
+ <Window.Resources>
+ <local:ViewModel x:Key="ViewModel"/>
+ </Window.Resources>
+ <Grid DataContext="{StaticResource ViewModel}">
+ <p:ItemsGrid x:Name="grid1" ItemsSource="{Binding DataTable501}" WrapItems="False">
+ </p:ItemsGrid>
+ </Grid>
+</Window>
diff -r a9cca0a755c4 -r f57b80cbd929 Source/Examples/ItemsGrid/FeaturesDemo/Examples/Window501.xaml.cs
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Source/Examples/ItemsGrid/FeaturesDemo/Examples/Window501.xaml.cs Fri Jul 12 14:08:19 2013 +0200
@@ -0,0 +1,45 @@
+// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="Window403.xaml.cs" company="PropertyTools">
+// The MIT License (MIT)
+//
+// Copyright (c) 2012 Oystein Bjorke
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+// </copyright>
+// <summary>
+// Interaction logic for Window403.xaml
+// </summary>
+// --------------------------------------------------------------------------------------------------------------------
+namespace FeaturesDemo
+{
+ /// <summary>
+ /// Interaction logic for Window403.xaml
+ /// </summary>
+ public partial class Window501
+ {
+ /// <summary>
+ /// Initializes a new instance of the <see cref="Window402"/> class.
+ /// </summary>
+ public Window501()
+ {
+ InitializeComponent();
+ }
+ }
+}
\ No newline at end of file
diff -r a9cca0a755c4 -r f57b80cbd929 Source/Examples/ItemsGrid/FeaturesDemo/FeaturesDemo_NET40.csproj
--- a/Source/Examples/ItemsGrid/FeaturesDemo/FeaturesDemo_NET40.csproj Fri Jan 11 08:35:23 2013 +0100
+++ b/Source/Examples/ItemsGrid/FeaturesDemo/FeaturesDemo_NET40.csproj Fri Jul 12 14:08:19 2013 +0200
@@ -50,6 +50,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
+ <Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="System.Xaml" />
@@ -62,6 +63,9 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
+ <Compile Include="Examples\Window501.xaml.cs">
+ <DependentUpon>Window501.xaml</DependentUpon>
+ </Compile>
<Compile Include="Examples\Window403.xaml.cs">
<DependentUpon>Window403.xaml</DependentUpon>
</Compile>
@@ -102,6 +106,10 @@
<Compile Include="DataTypes\Mass.cs" />
<Compile Include="DataTypes\MassConverter.cs" />
<Compile Include="Model\PlainOldObject.cs" />
+ <Page Include="Examples\Window501.xaml">
+ <Generator>MSBuild:Compile</Generator>
+ <SubType>Designer</SubType>
+ </Page>
<Page Include="Examples\Window403.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
diff -r a9cca0a755c4 -r f57b80cbd929 Source/Examples/ItemsGrid/FeaturesDemo/MainWindow.xaml
--- a/Source/Examples/ItemsGrid/FeaturesDemo/MainWindow.xaml Fri Jan 11 08:35:23 2013 +0100
+++ b/Source/Examples/ItemsGrid/FeaturesDemo/MainWindow.xaml Fri Jul 12 14:08:19 2013 +0200
@@ -23,5 +23,6 @@
<Button Content="double[]" Tag="{x:Type local:Window401}"/>
<Button Content="double[] (WrapItems=True)" Tag="{x:Type local:Window402}"/>
<Button Content="Mass[] (WrapItems=True)" Tag="{x:Type local:Window403}"/>
+ <Button Content="DataTable" Tag="{x:Type local:Window501}"/>
</StackPanel>
</Window>
diff -r a9cca0a755c4 -r f57b80cbd929 Source/PropertyTools.Wpf/ItemsGrid/Definitions/PropertyDefinition.cs
--- a/Source/PropertyTools.Wpf/ItemsGrid/Definitions/PropertyDefinition.cs Fri Jan 11 08:35:23 2013 +0100
+++ b/Source/PropertyTools.Wpf/ItemsGrid/Definitions/PropertyDefinition.cs Fri Jul 12 14:08:19 2013 +0200
@@ -254,6 +254,8 @@
/// </returns>
protected virtual string GetBindingPath(int index)
{
+ if (this.PropertyName != null)
+ return this.PropertyName;
return this.Descriptor != null ? this.Descriptor.Name : "[" + index + "]";
}
diff -r a9cca0a755c4 -r f57b80cbd929 Source/PropertyTools.Wpf/ItemsGrid/ItemsGrid.Content.cs
--- a/Source/PropertyTools.Wpf/ItemsGrid/ItemsGrid.Content.cs Fri Jan 11 08:35:23 2013 +0100
+++ b/Source/PropertyTools.Wpf/ItemsGrid/ItemsGrid.Content.cs Fri Jul 12 14:08:19 2013 +0200
@@ -27,6 +27,9 @@
// Represents a datagrid with a spreadsheet style.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
+
+using System.Data;
+
namespace PropertyTools.Wpf
{
using System.Collections.Specialized;
@@ -286,12 +289,15 @@
}
// If PropertyType is undefined, use the type of the items in the ItemsSource
- var itemsType = TypeHelper.GetItemType(this.ItemsSource);
- foreach (var pd in this.PropertyDefinitions)
+ if (!(this.ItemsSource is DataView))
{
- if (pd.PropertyType == null)
+ var itemsType = TypeHelper.GetItemType(this.ItemsSource);
+ foreach (var pd in this.PropertyDefinitions)
{
- pd.PropertyType = itemsType;
+ if (pd.PropertyType == null)
+ {
+ pd.PropertyType = itemsType;
+ }
}
}
@@ -299,18 +305,21 @@
this.ItemsInColumns = this.PropertyDefinitions.FirstOrDefault(pd => pd is RowDefinition) != null;
// If only PropertyName has been defined, use the type of the items to get the descriptor.
- var itemType = TypeHelper.GetItemType(this.ItemsSource);
- foreach (var pd in this.PropertyDefinitions)
+ if (!(this.ItemsSource is DataView))
{
- if (pd.Descriptor == null && !string.IsNullOrEmpty(pd.PropertyName))
+ var itemType = TypeHelper.GetItemType(this.ItemsSource);
+ foreach (var pd in this.PropertyDefinitions)
{
- pd.Descriptor = TypeDescriptor.GetProperties(itemType)[pd.PropertyName];
+ if (pd.Descriptor == null && !string.IsNullOrEmpty(pd.PropertyName))
+ {
+ pd.Descriptor = TypeDescriptor.GetProperties(itemType)[pd.PropertyName];
+ }
}
}
- int n = this.ItemsSource.Cast<object>().Count();
int m = this.PropertyDefinitions.Count;
-
+ int n = this.ItemsSource.Cast<object>().Count();
+
if (this.WrapItems)
{
n /= m;
diff -r a9cca0a755c4 -r f57b80cbd929 Source/PropertyTools.Wpf/ItemsGrid/ItemsGrid.cs
--- a/Source/PropertyTools.Wpf/ItemsGrid/ItemsGrid.cs Fri Jan 11 08:35:23 2013 +0100
+++ b/Source/PropertyTools.Wpf/ItemsGrid/ItemsGrid.cs Fri Jul 12 14:08:19 2013 +0200
@@ -27,6 +27,9 @@
// Represents a datagrid with a spreadsheet style.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
+
+using System.Data;
+
namespace PropertyTools.Wpf
{
using System;
@@ -604,6 +607,11 @@
{
return pd.Descriptor.GetValue(item);
}
+ else if (item is DataRowView)
+ {
+ var dv = item as DataRowView;
+ return dv[pd.PropertyName];
+ }
return item;
}
@@ -699,7 +707,15 @@
return false;
}
- var itemType = TypeHelper.GetItemType(list);
+ Type itemType;
+ if (list is DataView)
+ {
+ itemType = typeof (DataRow);
+ }
+ else
+ {
+ itemType = TypeHelper.GetItemType(list);
+ }
object newItem = null;
if (itemType == typeof(string))
@@ -1016,8 +1032,16 @@
this.currentEditor.SourceUpdated += this.CurrentCellSourceUpdated;
- this.SetElementDataContext(this.currentEditor, pd, item);
-
+ var rv = item as DataRowView;
+ if (rv != null)
+ {
+ this.SetElementDataContext(this.currentEditor, pd, rv);
+ }
+ else
+ {
+ this.SetElementDataContext(this.currentEditor, pd, item);
+ }
+
var textEditor = this.currentEditor as TextBox;
if (textEditor != null)
{
@@ -1137,6 +1161,11 @@
{
pd.Descriptor.SetValue(current, convertedValue);
}
+ else if (this.ItemsSource is DataView)
+ {
+ var dv = current as DataRowView;
+ dv[pd.PropertyName] = convertedValue;
+ }
else
{
var list = this.ItemsSource;
@@ -1195,8 +1224,16 @@
if (pd != null && item != null)
{
element = this.ControlFactory.CreateDisplayControl(pd, index);
- this.SetElementDataContext(element, pd, item);
-
+
+ var rv = item as DataRowView;
+ if (rv != null)
+ {
+ this.SetElementDataContext(element, pd, rv);
+ }
+ else
+ {
+ this.SetElementDataContext(element, pd, item);
+ }
element.SourceUpdated += this.CurrentCellSourceUpdated;
element.VerticalAlignment = VerticalAlignment.Center;
@@ -1233,52 +1270,71 @@
return;
}
- var itemType = TypeHelper.FindBiggestCommonType(items);
-
- var properties = TypeDescriptor.GetProperties(itemType);
-
- if (properties.Count == 0)
+ var dv = items as DataView;
+ if (dv == null)
{
- // Otherwise try to get the property descriptors from an instance
- foreach (var item in items)
+ var itemType = TypeHelper.FindBiggestCommonType(items);
+
+ var properties = TypeDescriptor.GetProperties(itemType);
+
+ if (properties.Count == 0)
{
- if (item != null && item.GetType() == itemType)
+ // Otherwise try to get the property descriptors from an instance
+ foreach (var item in items)
{
- properties = TypeDescriptor.GetProperties(item);
- break;
+ if (item != null && item.GetType() == itemType)
+ {
+ properties = TypeDescriptor.GetProperties(item);
+ break;
+ }
}
}
- }
-
- foreach (PropertyDescriptor descriptor in properties)
- {
- if (!descriptor.IsBrowsable)
+
+ foreach (PropertyDescriptor descriptor in properties)
{
- continue;
- }
-
- this.ColumnDefinitions.Add(
- new ColumnDefinition
+ if (!descriptor.IsBrowsable)
+ {
+ continue;
+ }
+
+ this.ColumnDefinitions.Add(
+ new ColumnDefinition
{
Descriptor = descriptor,
Header = descriptor.Name,
HorizontalAlignment = this.DefaultHorizontalAlignment,
Width = this.DefaultColumnWidth
});
- }
-
- var itemsType = TypeHelper.GetItemType(items);
- if (properties.Count == 0)
- {
- this.ColumnDefinitions.Add(
- new ColumnDefinition
+ }
+
+ var itemsType = TypeHelper.GetItemType(items);
+ if (properties.Count == 0)
+ {
+ this.ColumnDefinitions.Add(
+ new ColumnDefinition
{
PropertyType = itemsType,
Header = itemsType.Name,
HorizontalAlignment = this.DefaultHorizontalAlignment,
Width = this.DefaultColumnWidth
});
+ }
}
+ else
+ {
+ foreach (DataColumn col in dv.Table.Columns)
+ {
+ this.ColumnDefinitions.Add(
+ new ColumnDefinition
+ {
+ PropertyType = col.DataType,
+ Header = col.ColumnName,
+ PropertyName = col.ColumnName,
+ HorizontalAlignment = this.DefaultHorizontalAlignment,
+ Width = this.DefaultColumnWidth
+ });
+ }
+ }
}
/// <summary>
@@ -2791,7 +2847,7 @@
/// </param>
private void SetElementDataContext(FrameworkElement element, PropertyDefinition pd, object item)
{
- element.DataContext = pd.Descriptor != null ? item : this.ItemsSource;
+ element.DataContext = pd.Descriptor != null || item is DataRowView ? item : this.ItemsSource;
}
/// <summary>
diff -r a9cca0a755c4 -r f57b80cbd929 Source/PropertyTools.Wpf/PropertyTools.Wpf_NET40.csproj
--- a/Source/PropertyTools.Wpf/PropertyTools.Wpf_NET40.csproj Fri Jan 11 08:35:23 2013 +0100
+++ b/Source/PropertyTools.Wpf/PropertyTools.Wpf_NET40.csproj Fri Jul 12 14:08:19 2013 +0200
@@ -66,6 +66,7 @@
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
+ <Reference Include="System.Data" />
<Reference Include="System.Xaml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Xml" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment