Browse Source

Merge 3442cabed2 into 2c71faba5e

pull/1803/merge
Youri Karpeev 1 month ago
committed by GitHub
parent
commit
af3161cca5
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  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 namespace Xceed.Wpf.Toolkit
{ {
public class PrimitiveTypeCollectionControl : ContentControl public class PrimitiveTypeCollectionControl : ContentControl
{ {
#region Members #region Members
bool _surpressTextChanged; bool _surpressTextChanged;
bool _conversionFailed; 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 static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register("IsOpen", typeof(bool), typeof(PrimitiveTypeCollectionControl), new UIPropertyMetadata(false, OnIsOpenChanged));
public bool IsOpen public bool IsOpen
{ {
get get
{ {
return ( bool )GetValue( IsOpenProperty ); return (bool)GetValue(IsOpenProperty);
} }
set set
{ {
SetValue( IsOpenProperty, value ); SetValue(IsOpenProperty, value);
} }
} }
private static void OnIsOpenChanged( DependencyObject o, DependencyPropertyChangedEventArgs e ) private static void OnIsOpenChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{ {
PrimitiveTypeCollectionControl primitiveTypeCollectionControl = o as PrimitiveTypeCollectionControl; PrimitiveTypeCollectionControl primitiveTypeCollectionControl = o as PrimitiveTypeCollectionControl;
if( primitiveTypeCollectionControl != null ) if (primitiveTypeCollectionControl != null)
primitiveTypeCollectionControl.OnIsOpenChanged( ( bool )e.OldValue, ( bool )e.NewValue ); 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 static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IList), typeof(PrimitiveTypeCollectionControl), new UIPropertyMetadata(null, OnItemsSourceChanged));
public IList ItemsSource public IList ItemsSource
{ {
get get
{ {
return ( IList )GetValue( ItemsSourceProperty ); return (IList)GetValue(ItemsSourceProperty);
} }
set set
{ {
SetValue( ItemsSourceProperty, value ); SetValue(ItemsSourceProperty, value);
} }
} }
private static void OnItemsSourceChanged( DependencyObject o, DependencyPropertyChangedEventArgs e ) private static void OnItemsSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{ {
PrimitiveTypeCollectionControl primitiveTypeCollectionControl = o as PrimitiveTypeCollectionControl; PrimitiveTypeCollectionControl primitiveTypeCollectionControl = o as PrimitiveTypeCollectionControl;
if( primitiveTypeCollectionControl != null ) if (primitiveTypeCollectionControl != null)
primitiveTypeCollectionControl.OnItemsSourceChanged( ( IList )e.OldValue, ( IList )e.NewValue ); primitiveTypeCollectionControl.OnItemsSourceChanged((IList)e.OldValue, (IList)e.NewValue);
} }
protected virtual void OnItemsSourceChanged( IList oldValue, IList newValue ) protected virtual void OnItemsSourceChanged(IList oldValue, IList newValue)
{ {
if( newValue == null ) if (newValue == null)
return; return;
if( ItemsSourceType == null ) if (ItemsSourceType == null)
ItemsSourceType = newValue.GetType(); ItemsSourceType = newValue.GetType();
if( ItemType == null && newValue.GetType().ContainsGenericParameters ) if (ItemType == null && newValue.GetType().ContainsGenericParameters)
ItemType = newValue.GetType().GetGenericArguments()[ 0 ]; ItemType = newValue.GetType().GetGenericArguments()[0];
SetText( newValue ); SetText(newValue);
} }
#endregion //ItemsSource #endregion //ItemsSource
#region IsReadOnly #region IsReadOnly
public static readonly DependencyProperty IsReadOnlyProperty = public static readonly DependencyProperty IsReadOnlyProperty =
DependencyProperty.Register( "IsReadOnly", typeof( bool ), typeof( PrimitiveTypeCollectionControl ), new UIPropertyMetadata( false ) ); DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(PrimitiveTypeCollectionControl), new UIPropertyMetadata(false));
public bool IsReadOnly public bool IsReadOnly
{ {
get get
{ {
return ( bool )GetValue( IsReadOnlyProperty ); return (bool)GetValue(IsReadOnlyProperty);
} }
set set
{ {
SetValue( IsReadOnlyProperty, value ); 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 static readonly DependencyProperty ItemsSourceTypeProperty = DependencyProperty.Register("ItemsSourceType", typeof(Type), typeof(PrimitiveTypeCollectionControl), new UIPropertyMetadata(null));
public Type ItemsSourceType public Type ItemsSourceType
{ {
get get
{ {
return ( Type )GetValue( ItemsSourceTypeProperty ); return (Type)GetValue(ItemsSourceTypeProperty);
} }
set set
{ {
SetValue( ItemsSourceTypeProperty, value ); 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 static readonly DependencyProperty ItemTypeProperty = DependencyProperty.Register("ItemType", typeof(Type), typeof(PrimitiveTypeCollectionControl), new UIPropertyMetadata(null));
public Type ItemType public Type ItemType
{ {
get get
{ {
return ( Type )GetValue( ItemTypeProperty ); return (Type)GetValue(ItemTypeProperty);
} }
set set
{ {
SetValue( ItemTypeProperty, value ); 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 static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(PrimitiveTypeCollectionControl), new UIPropertyMetadata(null, OnTextChanged));
public string Text public string Text
{ {
get get
{ {
return ( string )GetValue( TextProperty ); return (string)GetValue(TextProperty);
} }
set set
{ {
SetValue( TextProperty, value ); SetValue(TextProperty, value);
} }
} }
private static void OnTextChanged( DependencyObject o, DependencyPropertyChangedEventArgs e ) private static void OnTextChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{ {
PrimitiveTypeCollectionControl primitiveTypeCollectionControl = o as PrimitiveTypeCollectionControl; PrimitiveTypeCollectionControl primitiveTypeCollectionControl = o as PrimitiveTypeCollectionControl;
if( primitiveTypeCollectionControl != null ) if (primitiveTypeCollectionControl != null)
primitiveTypeCollectionControl.OnTextChanged( ( string )e.OldValue, ( string )e.NewValue ); primitiveTypeCollectionControl.OnTextChanged((string)e.OldValue, (string)e.NewValue);
} }
protected virtual void OnTextChanged( string oldValue, string newValue ) protected virtual void OnTextChanged(string oldValue, string newValue)
{ {
if( !_surpressTextChanged ) if (!_surpressTextChanged)
PersistChanges(); PersistChanges();
} }
#endregion //Text #endregion //Text
#endregion //Properties #endregion //Properties
#region Constructors #region Constructors
static PrimitiveTypeCollectionControl() static PrimitiveTypeCollectionControl()
{ {
DefaultStyleKeyProperty.OverrideMetadata( typeof( PrimitiveTypeCollectionControl ), new FrameworkPropertyMetadata( typeof( 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() private void PersistChanges()
{ {
IList list = ComputeItemsSource(); IList list = ComputeItemsSource();
if( list == null ) if (list == null)
return; 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. //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(); list.Clear();
int counter = 0; int counter = 0;
foreach( var item in items ) foreach (var item in items)
{ {
if( list is Array ) if (list is Array)
{ {
( ( Array )list ).SetValue( item, counter++ ); ((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 (ItemType == null)
if( _conversionFailed ) return items;
SetText( list );
}
private IList ComputeItems() string[] textArray = Text.Split('\n');
{
IList items = new List<object>();
if( ItemType == null ) foreach (string s in textArray)
return items; {
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 ) private IList ComputeItemsSource()
{
string valueString = s.TrimEnd( '\r' );
if( !String.IsNullOrEmpty( valueString ) )
{ {
object value = null; if (ItemsSource == null)
try
{
if( ItemType.IsEnum )
{
value = Enum.Parse( ItemType, valueString );
}
else
{ {
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() private IList CreateItemsSource()
{ {
if( ItemsSource == null ) IList list = 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() if (ItemsSourceType != null)
{ {
IList list = null; ConstructorInfo constructor = ItemsSourceType.GetConstructor(Type.EmptyTypes);
list = (IList)constructor.Invoke(null);
}
if( ItemsSourceType != null ) return list;
{ }
ConstructorInfo constructor = ItemsSourceType.GetConstructor( Type.EmptyTypes );
list = ( IList )constructor.Invoke( null );
}
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 ) #endregion //Methods
{
_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
}
} }

Loading…
Cancel
Save