Browse Source

Allow grid row/cols in XAML as strings.

pull/114/head
Steven Kirk 11 years ago
parent
commit
a98cc89730
  1. 6
      samples/XamlTestApplication/Views/MainWindow.xaml
  2. 24
      src/Markup/Perspex.Markup.Xaml/Context/PerspexWiringContext.cs
  3. 36
      src/Markup/Perspex.Markup.Xaml/Converters/ColumnDefinitionsTypeConverter.cs
  4. 31
      src/Markup/Perspex.Markup.Xaml/Converters/GridLengthTypeConverter.cs
  5. 36
      src/Markup/Perspex.Markup.Xaml/Converters/RowDefinitionsTypeConverter.cs
  6. 2
      src/Markup/Perspex.Markup.Xaml/Perspex.Markup.Xaml.csproj

6
samples/XamlTestApplication/Views/MainWindow.xaml

@ -2,11 +2,7 @@
xmlns="https://github.com/grokys/Perspex"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Perspex Test Application" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid RowDefinitions="Auto,*">
<TabControl Grid.Row="1">
<TabItem Header="Buttons">
<StackPanel HorizontalAlignment="Center" Width="200" VerticalAlignment="Center">

24
src/Markup/Perspex.Markup.Xaml/Context/PerspexWiringContext.cs

@ -83,10 +83,12 @@ namespace Perspex.Markup.Xaml.Context
var typeConverterProvider = new TypeConverterProvider();
var converters = new[]
{
new TypeConverterRegistration(typeof (Bitmap), new BitmapConverter()),
new TypeConverterRegistration(typeof (GridLength), new GridLengthTypeConverter()),
new TypeConverterRegistration(typeof (Brush), new BrushConverter()),
new TypeConverterRegistration(typeof (Thickness), new ThicknessConverter()),
new TypeConverterRegistration(typeof(Bitmap), new BitmapConverter()),
new TypeConverterRegistration(typeof(Brush), new BrushConverter()),
new TypeConverterRegistration(typeof(ColumnDefinitions), new ColumnDefinitionsTypeConverter()),
new TypeConverterRegistration(typeof(GridLength), new GridLengthTypeConverter()),
new TypeConverterRegistration(typeof(RowDefinitions), new RowDefinitionsTypeConverter()),
new TypeConverterRegistration(typeof(Thickness), new ThicknessConverter()),
};
typeConverterProvider.AddAll(converters);
@ -98,13 +100,13 @@ namespace Perspex.Markup.Xaml.Context
var contentPropertyProvider = new ContentPropertyProvider();
var contentProperties = new Collection<ContentPropertyDefinition>
{
new ContentPropertyDefinition(typeof (ContentControl), "Content"),
new ContentPropertyDefinition(typeof (Decorator), "Child"),
new ContentPropertyDefinition(typeof (ItemsControl), "Items"),
new ContentPropertyDefinition(typeof (Panel), "Children"),
new ContentPropertyDefinition(typeof (TextBlock), "Text"),
new ContentPropertyDefinition(typeof (TextBox), "Text"),
new ContentPropertyDefinition(typeof (XamlDataTemplate), "Content"),
new ContentPropertyDefinition(typeof(ContentControl), "Content"),
new ContentPropertyDefinition(typeof(Decorator), "Child"),
new ContentPropertyDefinition(typeof(ItemsControl), "Items"),
new ContentPropertyDefinition(typeof(Panel), "Children"),
new ContentPropertyDefinition(typeof(TextBlock), "Text"),
new ContentPropertyDefinition(typeof(TextBox), "Text"),
new ContentPropertyDefinition(typeof(XamlDataTemplate), "Content"),
};
contentPropertyProvider.AddAll(contentProperties);

36
src/Markup/Perspex.Markup.Xaml/Converters/ColumnDefinitionsTypeConverter.cs

@ -0,0 +1,36 @@
// -----------------------------------------------------------------------
// <copyright file="ColumnDefinitionsTypeConverter.cs" company="Steven Kirk">
// Copyright 2015 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Markup.Xaml.Converters
{
using System;
using System.Globalization;
using Controls;
using OmniXaml.TypeConversion;
public class ColumnDefinitionsTypeConverter : ITypeConverter
{
public bool CanConvertFrom(IXamlTypeConverterContext context, Type sourceType)
{
return sourceType == typeof(string);
}
public bool CanConvertTo(IXamlTypeConverterContext context, Type destinationType)
{
return false;
}
public object ConvertFrom(IXamlTypeConverterContext context, CultureInfo culture, object value)
{
return new ColumnDefinitions((string)value);
}
public object ConvertTo(IXamlTypeConverterContext context, CultureInfo culture, object value, Type destinationType)
{
throw new NotImplementedException();
}
}
}

31
src/Markup/Perspex.Markup.Xaml/Converters/GridLengthTypeConverter.cs

@ -13,39 +13,24 @@ namespace Perspex.Markup.Xaml.Converters
public class GridLengthTypeConverter : ITypeConverter
{
public object ConvertFrom(IXamlTypeConverterContext context, CultureInfo culture, object value)
public bool CanConvertFrom(IXamlTypeConverterContext context, Type sourceType)
{
var str = value as string;
if (str != null)
{
if (string.Equals(str, "Auto"))
{
return new GridLength(0, GridUnitType.Auto);
}
}
return new GridLength(1, GridUnitType.Star);
return sourceType == typeof(string);
}
public object ConvertTo(IXamlTypeConverterContext context, CultureInfo culture, object value, Type destinationType)
public bool CanConvertTo(IXamlTypeConverterContext context, Type destinationType)
{
if ((string) value == "Auto")
{
return new GridLength(0, GridUnitType.Auto);
}
return new GridLength(1, GridUnitType.Star);
return false;
}
public bool CanConvertTo(IXamlTypeConverterContext context, Type destinationType)
public object ConvertFrom(IXamlTypeConverterContext context, CultureInfo culture, object value)
{
return true;
return GridLength.Parse((string)value);
}
public bool CanConvertFrom(IXamlTypeConverterContext context, Type sourceType)
public object ConvertTo(IXamlTypeConverterContext context, CultureInfo culture, object value, Type destinationType)
{
return true;
throw new NotImplementedException();
}
}
}

36
src/Markup/Perspex.Markup.Xaml/Converters/RowDefinitionsTypeConverter.cs

@ -0,0 +1,36 @@
// -----------------------------------------------------------------------
// <copyright file="RowDefinitionsTypeConverter.cs" company="Steven Kirk">
// Copyright 2015 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Markup.Xaml.Converters
{
using System;
using System.Globalization;
using Controls;
using OmniXaml.TypeConversion;
public class RowDefinitionsTypeConverter : ITypeConverter
{
public bool CanConvertFrom(IXamlTypeConverterContext context, Type sourceType)
{
return sourceType == typeof(string);
}
public bool CanConvertTo(IXamlTypeConverterContext context, Type destinationType)
{
return false;
}
public object ConvertFrom(IXamlTypeConverterContext context, CultureInfo culture, object value)
{
return new RowDefinitions((string)value);
}
public object ConvertTo(IXamlTypeConverterContext context, CultureInfo culture, object value, Type destinationType)
{
throw new NotImplementedException();
}
}
}

2
src/Markup/Perspex.Markup.Xaml/Perspex.Markup.Xaml.csproj

@ -38,6 +38,8 @@
<Compile Include="..\..\Shared\SharedAssemblyInfo.cs">
<Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Converters\RowDefinitionsTypeConverter.cs" />
<Compile Include="Converters\ColumnDefinitionsTypeConverter.cs" />
<Compile Include="Converters\ThicknessConverter.cs" />
<Compile Include="Context\PerspexWiringContext.cs" />
<Compile Include="GlobalSuppressions.cs" />

Loading…
Cancel
Save