Browse Source

PropertyGrid: added demo to sample application on how to deal with binding to Structs

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
40e30adeea
  1. 1
      ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/PropertyGridModule.cs
  2. 7
      ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Samples.Modules.PropertyGrid.csproj
  3. 51
      ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Views/BindingToStructs.xaml
  4. 96
      ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Views/BindingToStructs.xaml.cs
  5. 2
      ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Views/CustomEditors.xaml.cs
  6. 2
      ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Views/CustomItemsSource.xaml.cs
  7. 2
      ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Views/DefaultEditors.xaml.cs
  8. 3
      ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Views/NavigationView.xaml
  9. 2
      ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Views/SpecifyingProperties.xaml.cs

1
ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/PropertyGridModule.cs

@ -25,6 +25,7 @@ namespace Samples.Modules.PropertyGrid
Container.RegisterNavigationType(typeof(DefaultEditors));
Container.RegisterNavigationType(typeof(ExpandableProperties));
Container.RegisterNavigationType(typeof(SpecifyingProperties));
Container.RegisterNavigationType(typeof(BindingToStructs));
}
}
}

7
ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Samples.Modules.PropertyGrid.csproj

@ -78,6 +78,9 @@
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<Compile Include="Views\BindingToStructs.xaml.cs">
<DependentUpon>BindingToStructs.xaml</DependentUpon>
</Compile>
<Compile Include="Views\CustomEditors.xaml.cs">
<DependentUpon>CustomEditors.xaml</DependentUpon>
</Compile>
@ -124,6 +127,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\BindingToStructs.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\CustomEditors.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>

51
ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Views/BindingToStructs.xaml

@ -0,0 +1,51 @@
<infControls:DemoView x:Class="Samples.Modules.PropertyGrid.Views.BindingToStructs"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:infControls="clr-namespace:Samples.Infrastructure.Controls;assembly=Samples.Infrastructure"
xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
xmlns:local="clr-namespace:Samples.Modules.PropertyGrid.Views"
Title="Binding to Structs">
<infControls:DemoView.Resources>
<local:DimensionsConverter x:Key="DimensionsConverter" />
</infControls:DemoView.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="When the SelectedObject contains properties of type Struct youmay find that edits performed in the PropertyGrid do not function properly. This is because Structs are passed by value. Meaning that the PropertyGrid receives a copy of the Struct and not a reference. When this happens, the data binding expressions end up binding to and modifying that copy rather than the original values."
TextWrapping="Wrap" />
<StackPanel Grid.Row="1" Margin="10">
<TextBlock Text="Binding to Structs:" Style="{StaticResource Header}" />
<TextBlock Text="The solution to this problem is to create a custom editor and provide an IValueConverter to handle the proper conversion of the Struct values." TextWrapping="Wrap" />
<extToolkit:PropertyGrid x:Name="_propertyGrid" Width="450" Margin="10" >
<extToolkit:PropertyGrid.EditorDefinitions>
<extToolkit:EditorDefinition TargetType="{x:Type local:Dimension}">
<extToolkit:EditorDefinition.EditorTemplate>
<DataTemplate>
<Grid Margin="5" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="Height: " />
<TextBox Grid.Column="1" Text="{Binding Path=Value, Converter={StaticResource DimensionsConverter},ConverterParameter=Height}" />
<TextBlock Grid.Row="1" Text="Weight: " />
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Path=Value, Converter={StaticResource DimensionsConverter},ConverterParameter=Weight}" />
</Grid>
</DataTemplate>
</extToolkit:EditorDefinition.EditorTemplate>
</extToolkit:EditorDefinition>
</extToolkit:PropertyGrid.EditorDefinitions>
</extToolkit:PropertyGrid>
</StackPanel>
</Grid>
</infControls:DemoView>

96
ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Views/BindingToStructs.xaml.cs

@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Samples.Infrastructure.Controls;
using Microsoft.Practices.Prism.Regions;
using System.ComponentModel;
namespace Samples.Modules.PropertyGrid.Views
{
/// <summary>
/// Interaction logic for BindingToStructs.xaml
/// </summary>
[RegionMemberLifetime(KeepAlive = false)]
public partial class BindingToStructs : DemoView
{
public BindingToStructs()
{
InitializeComponent();
_propertyGrid.SelectedObject = Person.CreatePerson();
}
public class Person
{
[Category("Information")]
[DisplayName("First Name")]
[Description("This property uses a TextBox as the default editor.")]
public string FirstName { get; set; }
[Category("Information")]
[DisplayName("Last Name")]
[Description("This property uses a TextBox as the default editor.")]
public string LastName { get; set; }
public Dimension Dimensions { get; set; }
public static Person CreatePerson()
{
var person = new Person();
person.FirstName = "John";
person.LastName = "Doe";
person.Dimensions = new Dimension() { Height = 75.0, Weight = 185.76 };
return person;
}
}
}
public struct Dimension
{
public double Height;
public double Weight;
public Dimension(double height, double weight)
{
this.Height = height;
this.Weight = weight;
}
}
public class DimensionsConverter : IValueConverter
{
static Dimension _originalValue; // the static struct that stores original value at the start of editing
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
_originalValue = ((Dimension)value);
if (parameter.ToString() == "Height")
return ((Dimension)value).Height;
if (parameter.ToString() == "Weight")
return ((Dimension)value).Weight;
return _originalValue;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (parameter.ToString() == "Height")
_originalValue = new Dimension(double.Parse(value.ToString()), _originalValue.Weight);
if (parameter.ToString() == "Weight")
_originalValue = new Dimension(_originalValue.Height, double.Parse(value.ToString()));
return _originalValue;
}
}
}

2
ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Views/CustomEditors.xaml.cs

@ -6,12 +6,14 @@ using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.Practices.Prism.Regions;
namespace Samples.Modules.PropertyGrid.Views
{
/// <summary>
/// Interaction logic for CustomEditors.xaml
/// </summary>
[RegionMemberLifetime(KeepAlive = false)]
public partial class CustomEditors : DemoView
{
public CustomEditors()

2
ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Views/CustomItemsSource.xaml.cs

@ -5,12 +5,14 @@ using System;
using System.Windows.Media;
using System.Windows;
using Microsoft.Windows.Controls.PropertyGrid.Attributes;
using Microsoft.Practices.Prism.Regions;
namespace Samples.Modules.PropertyGrid.Views
{
/// <summary>
/// Interaction logic for CustomItemsSource.xaml
/// </summary>
[RegionMemberLifetime(KeepAlive = false)]
public partial class CustomItemsSource : DemoView
{
public CustomItemsSource()

2
ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Views/DefaultEditors.xaml.cs

@ -4,12 +4,14 @@ using System.Collections.Generic;
using System.Windows.Media;
using System.ComponentModel;
using System.Windows;
using Microsoft.Practices.Prism.Regions;
namespace Samples.Modules.PropertyGrid.Views
{
/// <summary>
/// Interaction logic for DefaultEditors.xaml
/// </summary>
[RegionMemberLifetime(KeepAlive = false)]
public partial class DefaultEditors : DemoView
{
public DefaultEditors()

3
ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Views/NavigationView.xaml

@ -13,9 +13,10 @@
<TreeViewItem Header="Custom Editors" Tag="{x:Type views:CustomEditors}"/>
</TreeViewItem>
<TreeViewItem Header="Properties">
<TreeViewItem Header="Binding to Structs" Tag="{x:Type views:BindingToStructs}"/>
<TreeViewItem Header="Expandable Properties" Tag="{x:Type views:ExpandableProperties}"/>
<TreeViewItem Header="Specifying Properties" Tag="{x:Type views:SpecifyingProperties}"/>
<TreeViewItem Header="Providing an ItemsSource" Tag="{x:Type views:CustomItemsSource}"/>
<TreeViewItem Header="Specifying Properties" Tag="{x:Type views:SpecifyingProperties}"/>
</TreeViewItem>
</TreeViewItem>

2
ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Views/SpecifyingProperties.xaml.cs

@ -4,12 +4,14 @@ using System.Collections.Generic;
using System;
using System.Windows.Media;
using System.Windows;
using Microsoft.Practices.Prism.Regions;
namespace Samples.Modules.PropertyGrid.Views
{
/// <summary>
/// Interaction logic for SpecifyingProperties.xaml
/// </summary>
[RegionMemberLifetime(KeepAlive = false)]
public partial class SpecifyingProperties : DemoView
{
public SpecifyingProperties()

Loading…
Cancel
Save