Browse Source

when persisting changes in PrimitiveTypeCollectionControl update the itemssource - allows to see the change in view model

pull/1803/head
youri 6 months ago
parent
commit
3442cabed2
  1. 466
      ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/CollectionControl/Implementation/PrimitiveTypeCollectionControl.cs

466
ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/CollectionControl/Implementation/PrimitiveTypeCollectionControl.cs

@ -25,299 +25,301 @@ using System.Windows.Controls;
namespace Xceed.Wpf.Toolkit
{
public class PrimitiveTypeCollectionControl : ContentControl
{
#region Members
public class PrimitiveTypeCollectionControl : ContentControl
{
#region Members
bool _surpressTextChanged;
bool _conversionFailed;
bool _surpressTextChanged;
bool _conversionFailed;
#endregion //Members
#endregion //Members
#region Properties
#region Properties
#region IsOpen
#region IsOpen
public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register( "IsOpen", typeof( bool ), typeof( PrimitiveTypeCollectionControl ), new UIPropertyMetadata( false, OnIsOpenChanged ) );
public bool IsOpen
{
get
{
return ( bool )GetValue( IsOpenProperty );
}
set
{
SetValue( IsOpenProperty, value );
}
}
public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register("IsOpen", typeof(bool), typeof(PrimitiveTypeCollectionControl), new UIPropertyMetadata(false, OnIsOpenChanged));
public bool IsOpen
{
get
{
return (bool)GetValue(IsOpenProperty);
}
set
{
SetValue(IsOpenProperty, value);
}
}
private static void OnIsOpenChanged( DependencyObject o, DependencyPropertyChangedEventArgs e )
{
PrimitiveTypeCollectionControl primitiveTypeCollectionControl = o as PrimitiveTypeCollectionControl;
if( primitiveTypeCollectionControl != null )
primitiveTypeCollectionControl.OnIsOpenChanged( ( bool )e.OldValue, ( bool )e.NewValue );
}
private static void OnIsOpenChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
PrimitiveTypeCollectionControl primitiveTypeCollectionControl = o as PrimitiveTypeCollectionControl;
if (primitiveTypeCollectionControl != null)
primitiveTypeCollectionControl.OnIsOpenChanged((bool)e.OldValue, (bool)e.NewValue);
}
protected virtual void OnIsOpenChanged( bool oldValue, bool newValue )
{
protected virtual void OnIsOpenChanged(bool oldValue, bool newValue)
{
}
}
#endregion //IsOpen
#endregion //IsOpen
#region ItemsSource
#region ItemsSource
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register( "ItemsSource", typeof( IList ), typeof( PrimitiveTypeCollectionControl ), new UIPropertyMetadata( null, OnItemsSourceChanged ) );
public IList ItemsSource
{
get
{
return ( IList )GetValue( ItemsSourceProperty );
}
set
{
SetValue( ItemsSourceProperty, value );
}
}
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IList), typeof(PrimitiveTypeCollectionControl), new UIPropertyMetadata(null, OnItemsSourceChanged));
public IList ItemsSource
{
get
{
return (IList)GetValue(ItemsSourceProperty);
}
set
{
SetValue(ItemsSourceProperty, value);
}
}
private static void OnItemsSourceChanged( DependencyObject o, DependencyPropertyChangedEventArgs e )
{
PrimitiveTypeCollectionControl primitiveTypeCollectionControl = o as PrimitiveTypeCollectionControl;
if( primitiveTypeCollectionControl != null )
primitiveTypeCollectionControl.OnItemsSourceChanged( ( IList )e.OldValue, ( IList )e.NewValue );
}
private static void OnItemsSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
PrimitiveTypeCollectionControl primitiveTypeCollectionControl = o as PrimitiveTypeCollectionControl;
if (primitiveTypeCollectionControl != null)
primitiveTypeCollectionControl.OnItemsSourceChanged((IList)e.OldValue, (IList)e.NewValue);
}
protected virtual void OnItemsSourceChanged( IList oldValue, IList newValue )
{
if( newValue == null )
return;
protected virtual void OnItemsSourceChanged(IList oldValue, IList newValue)
{
if (newValue == null)
return;
if( ItemsSourceType == null )
ItemsSourceType = newValue.GetType();
if (ItemsSourceType == null)
ItemsSourceType = newValue.GetType();
if( ItemType == null && newValue.GetType().ContainsGenericParameters )
ItemType = newValue.GetType().GetGenericArguments()[ 0 ];
if (ItemType == null && newValue.GetType().ContainsGenericParameters)
ItemType = newValue.GetType().GetGenericArguments()[0];
SetText( newValue );
}
SetText(newValue);
}
#endregion //ItemsSource
#endregion //ItemsSource
#region IsReadOnly
#region IsReadOnly
public static readonly DependencyProperty IsReadOnlyProperty =
DependencyProperty.Register( "IsReadOnly", typeof( bool ), typeof( PrimitiveTypeCollectionControl ), new UIPropertyMetadata( false ) );
public static readonly DependencyProperty IsReadOnlyProperty =
DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(PrimitiveTypeCollectionControl), new UIPropertyMetadata(false));
public bool IsReadOnly
{
get
{
return ( bool )GetValue( IsReadOnlyProperty );
}
set
{
SetValue( IsReadOnlyProperty, value );
}
}
public bool IsReadOnly
{
get
{
return (bool)GetValue(IsReadOnlyProperty);
}
set
{
SetValue(IsReadOnlyProperty, value);
}
}
#endregion //IsReadOnly
#endregion //IsReadOnly
#region ItemsSourceType
#region ItemsSourceType
public static readonly DependencyProperty ItemsSourceTypeProperty = DependencyProperty.Register( "ItemsSourceType", typeof( Type ), typeof( PrimitiveTypeCollectionControl ), new UIPropertyMetadata( null ) );
public Type ItemsSourceType
{
get
{
return ( Type )GetValue( ItemsSourceTypeProperty );
}
set
{
SetValue( ItemsSourceTypeProperty, value );
}
}
public static readonly DependencyProperty ItemsSourceTypeProperty = DependencyProperty.Register("ItemsSourceType", typeof(Type), typeof(PrimitiveTypeCollectionControl), new UIPropertyMetadata(null));
public Type ItemsSourceType
{
get
{
return (Type)GetValue(ItemsSourceTypeProperty);
}
set
{
SetValue(ItemsSourceTypeProperty, value);
}
}
#endregion ItemsSourceType
#endregion ItemsSourceType
#region ItemType
#region ItemType
public static readonly DependencyProperty ItemTypeProperty = DependencyProperty.Register( "ItemType", typeof( Type ), typeof( PrimitiveTypeCollectionControl ), new UIPropertyMetadata( null ) );
public Type ItemType
{
get
{
return ( Type )GetValue( ItemTypeProperty );
}
set
{
SetValue( ItemTypeProperty, value );
}
}
public static readonly DependencyProperty ItemTypeProperty = DependencyProperty.Register("ItemType", typeof(Type), typeof(PrimitiveTypeCollectionControl), new UIPropertyMetadata(null));
public Type ItemType
{
get
{
return (Type)GetValue(ItemTypeProperty);
}
set
{
SetValue(ItemTypeProperty, value);
}
}
#endregion ItemType
#endregion ItemType
#region Text
#region Text
public static readonly DependencyProperty TextProperty = DependencyProperty.Register( "Text", typeof( string ), typeof( PrimitiveTypeCollectionControl ), new UIPropertyMetadata( null, OnTextChanged ) );
public string Text
{
get
{
return ( string )GetValue( TextProperty );
}
set
{
SetValue( TextProperty, value );
}
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(PrimitiveTypeCollectionControl), new UIPropertyMetadata(null, OnTextChanged));
public string Text
{
get
{
return (string)GetValue(TextProperty);
}
set
{
SetValue(TextProperty, value);
}
}
private static void OnTextChanged( DependencyObject o, DependencyPropertyChangedEventArgs e )
{
PrimitiveTypeCollectionControl primitiveTypeCollectionControl = o as PrimitiveTypeCollectionControl;
if( primitiveTypeCollectionControl != null )
primitiveTypeCollectionControl.OnTextChanged( ( string )e.OldValue, ( string )e.NewValue );
}
private static void OnTextChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
PrimitiveTypeCollectionControl primitiveTypeCollectionControl = o as PrimitiveTypeCollectionControl;
if (primitiveTypeCollectionControl != null)
primitiveTypeCollectionControl.OnTextChanged((string)e.OldValue, (string)e.NewValue);
}
protected virtual void OnTextChanged( string oldValue, string newValue )
{
if( !_surpressTextChanged )
PersistChanges();
}
protected virtual void OnTextChanged(string oldValue, string newValue)
{
if (!_surpressTextChanged)
PersistChanges();
}
#endregion //Text
#endregion //Text
#endregion //Properties
#endregion //Properties
#region Constructors
#region Constructors
static PrimitiveTypeCollectionControl()
{
DefaultStyleKeyProperty.OverrideMetadata( typeof( PrimitiveTypeCollectionControl ), new FrameworkPropertyMetadata( typeof( PrimitiveTypeCollectionControl ) ) );
}
static PrimitiveTypeCollectionControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(PrimitiveTypeCollectionControl), new FrameworkPropertyMetadata(typeof(PrimitiveTypeCollectionControl)));
}
public PrimitiveTypeCollectionControl()
{
public PrimitiveTypeCollectionControl()
{
Core.Message.ShowMessage();
}
Core.Message.ShowMessage();
}
#endregion //Constructors
#endregion //Constructors
#region Overrides
#region Overrides
#endregion
#endregion
#region Methods
#region Methods
private void PersistChanges()
{
IList list = ComputeItemsSource();
if( list == null )
return;
private void PersistChanges()
{
IList list = ComputeItemsSource();
if (list == null)
return;
IList items = ComputeItems();
IList items = ComputeItems();
//the easiest way to persist changes to the source is to just clear the source list and then add all items to it.
list.Clear();
//the easiest way to persist changes to the source is to just clear the source list and then add all items to it.
list.Clear();
int counter = 0;
foreach( var item in items )
{
if( list is Array )
{
( ( Array )list ).SetValue( item, counter++ );
int counter = 0;
foreach (var item in items)
{
if (list is Array)
{
((Array)list).SetValue(item, counter++);
}
else
{
list.Add(item);
}
}
this.ItemsSource = list;
// if something went wrong during conversion we want to reload the text to show only valid entries
if (_conversionFailed)
SetText(list);
}
else
private IList ComputeItems()
{
list.Add( item );
}
};
IList items = new List<object>();
// if something went wrong during conversion we want to reload the text to show only valid entries
if( _conversionFailed )
SetText( list );
}
if (ItemType == null)
return items;
private IList ComputeItems()
{
IList items = new List<object>();
string[] textArray = Text.Split('\n');
if( ItemType == null )
return items;
foreach (string s in textArray)
{
string valueString = s.TrimEnd('\r');
if (!String.IsNullOrEmpty(valueString))
{
object value = null;
try
{
if (ItemType.IsEnum)
{
value = Enum.Parse(ItemType, valueString);
}
else
{
value = Convert.ChangeType(valueString, ItemType);
}
}
catch
{
//a conversion failed
_conversionFailed = true;
}
if (value != null)
items.Add(value);
}
}
string[] textArray = Text.Split( '\n' );
return items;
}
foreach( string s in textArray )
{
string valueString = s.TrimEnd( '\r' );
if( !String.IsNullOrEmpty( valueString ) )
private IList ComputeItemsSource()
{
object value = null;
try
{
if( ItemType.IsEnum )
{
value = Enum.Parse( ItemType, valueString );
}
else
if (ItemsSource == null)
{
value = Convert.ChangeType( valueString, ItemType );
// Save current text since creating the ItemsSource will reset it
string currentText = this.Text;
ItemsSource = CreateItemsSource();
this.Text = currentText;
}
}
catch
{
//a conversion failed
_conversionFailed = true;
}
if( value != null )
items.Add( value );
}
}
return items;
}
return ItemsSource;
}
private IList ComputeItemsSource()
{
if( ItemsSource == null )
{
// Save current text since creating the ItemsSource will reset it
string currentText = this.Text;
ItemsSource = CreateItemsSource();
this.Text = currentText;
}
return ItemsSource;
}
private IList CreateItemsSource()
{
IList list = null;
private IList CreateItemsSource()
{
IList list = null;
if (ItemsSourceType != null)
{
ConstructorInfo constructor = ItemsSourceType.GetConstructor(Type.EmptyTypes);
list = (IList)constructor.Invoke(null);
}
if( ItemsSourceType != null )
{
ConstructorInfo constructor = ItemsSourceType.GetConstructor( Type.EmptyTypes );
list = ( IList )constructor.Invoke( null );
}
return list;
}
return list;
}
private void SetText(IEnumerable collection)
{
_surpressTextChanged = true;
StringBuilder builder = new StringBuilder();
foreach (object obj2 in collection)
{
builder.Append(obj2.ToString());
builder.AppendLine();
}
Text = builder.ToString().Trim();
_surpressTextChanged = false;
}
private void SetText( IEnumerable collection )
{
_surpressTextChanged = true;
StringBuilder builder = new StringBuilder();
foreach( object obj2 in collection )
{
builder.Append( obj2.ToString() );
builder.AppendLine();
}
Text = builder.ToString().Trim();
_surpressTextChanged = false;
#endregion //Methods
}
#endregion //Methods
}
}

Loading…
Cancel
Save