15 changed files with 330 additions and 43 deletions
@ -1,22 +1,29 @@ |
|||
<Window xmlns="https://github.com/perspex"> |
|||
<StackPanel Orientation="Horizontal"> |
|||
<StackPanel Margin="18" Gap="4" Width="200"> |
|||
<TextBlock FontSize="16" Text="Simple Bindings"/> |
|||
<TextBox Watermark="Two Way" UseFloatingWatermark="True" Text="{Binding StringValue}"/> |
|||
<TextBox Watermark="One Way" UseFloatingWatermark="True" Text="{Binding StringValue, Mode=OneWay}"/> |
|||
<TextBox Watermark="One Time" UseFloatingWatermark="True" Text="{Binding StringValue, Mode=OneTime}"/> |
|||
<TextBox Watermark="One Way To Source" UseFloatingWatermark="True" Text="{Binding StringValue, Mode=OneWayToSource}"/> |
|||
<StackPanel Orientation="Vertical"> |
|||
<StackPanel Orientation="Horizontal"> |
|||
<StackPanel Margin="18" Gap="4" Width="200"> |
|||
<TextBlock FontSize="16" Text="Simple Bindings"/> |
|||
<TextBox Watermark="Two Way" UseFloatingWatermark="True" Text="{Binding StringValue}"/> |
|||
<TextBox Watermark="One Way" UseFloatingWatermark="True" Text="{Binding StringValue, Mode=OneWay}"/> |
|||
<TextBox Watermark="One Time" UseFloatingWatermark="True" Text="{Binding StringValue, Mode=OneTime}"/> |
|||
<TextBox Watermark="One Way To Source" UseFloatingWatermark="True" Text="{Binding StringValue, Mode=OneWayToSource}"/> |
|||
</StackPanel> |
|||
<StackPanel Margin="18" Gap="4" Width="200"> |
|||
<TextBlock FontSize="16" Text="Collection Bindings"/> |
|||
<TextBox Watermark="Items[1].StringValue" UseFloatingWatermark="True" Text="{Binding Items[1].StringValue}"/> |
|||
<Button Command="{Binding ShuffleItems}">Shuffle</Button> |
|||
</StackPanel> |
|||
<StackPanel Margin="18" Gap="4" Width="200"> |
|||
<TextBlock FontSize="16" Text="Negated Bindings"/> |
|||
<TextBox Watermark="Boolean String" UseFloatingWatermark="True" Text="{Binding BooleanString}"/> |
|||
<CheckBox IsChecked="{Binding !BooleanString}">!BooleanString</CheckBox> |
|||
<CheckBox IsChecked="{Binding !!BooleanString}">!!BooleanString</CheckBox> |
|||
</StackPanel> |
|||
</StackPanel> |
|||
<StackPanel Margin="18" Gap="4" Width="200"> |
|||
<TextBlock FontSize="16" Text="Collection Bindings"/> |
|||
<TextBox Watermark="Items[1].StringValue" UseFloatingWatermark="True" Text="{Binding Items[1].StringValue}"/> |
|||
<Button Command="{Binding ShuffleItems}">Shuffle</Button> |
|||
</StackPanel> |
|||
<StackPanel Margin="18" Gap="4" Width="200"> |
|||
<TextBlock FontSize="16" Text="Negated Bindings"/> |
|||
<TextBox Watermark="Boolean String" UseFloatingWatermark="True" Text="{Binding BooleanString}"/> |
|||
<CheckBox IsChecked="{Binding !BooleanString}">!BooleanString</CheckBox> |
|||
<CheckBox IsChecked="{Binding !!BooleanString}">!!BooleanString</CheckBox> |
|||
<StackPanel Margin="18" Gap="4" Width="200" HorizontalAlignment="Left"> |
|||
<TextBlock FontSize="16" Text="Numeric Bindings"/> |
|||
<TextBox Watermark="Double" UseFloatingWatermark="True" Text="{Binding DoubleValue, Mode=TwoWay}"/> |
|||
<TextBlock Text="{Binding DoubleValue}"/> |
|||
</StackPanel> |
|||
</StackPanel> |
|||
</Window> |
|||
@ -0,0 +1,56 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System; |
|||
using System.Globalization; |
|||
using Perspex.Utilities; |
|||
|
|||
namespace Perspex.Markup |
|||
{ |
|||
/// <summary>
|
|||
/// Provides a default set of value conversions for bindings that do not specify a value
|
|||
/// converter.
|
|||
/// </summary>
|
|||
public class DefaultValueConverter : IValueConverter |
|||
{ |
|||
/// <summary>
|
|||
/// Gets an instance of a <see cref="DefaultValueConverter"/>.
|
|||
/// </summary>
|
|||
public static readonly DefaultValueConverter Instance = new DefaultValueConverter(); |
|||
|
|||
/// <summary>
|
|||
/// Converts a value.
|
|||
/// </summary>
|
|||
/// <param name="value">The value to convert.</param>
|
|||
/// <param name="targetType">The type of the target.</param>
|
|||
/// <param name="parameter">A user-defined parameter.</param>
|
|||
/// <param name="culture">The culture to use.</param>
|
|||
/// <returns>The converted value.</returns>
|
|||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
|||
{ |
|||
object result; |
|||
|
|||
if (TypeUtilities.TryConvert(targetType, value, culture, out result)) |
|||
{ |
|||
return result; |
|||
} |
|||
else |
|||
{ |
|||
throw new InvalidCastException($"Cannot convert value from {value.GetType()} to {targetType}"); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts a value.
|
|||
/// </summary>
|
|||
/// <param name="value">The value to convert.</param>
|
|||
/// <param name="targetType">The type of the target.</param>
|
|||
/// <param name="parameter">A user-defined parameter.</param>
|
|||
/// <param name="culture">The culture to use.</param>
|
|||
/// <returns>The converted value.</returns>
|
|||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
|||
{ |
|||
return Convert(value, targetType, parameter, culture); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System; |
|||
using System.Globalization; |
|||
|
|||
namespace Perspex.Markup |
|||
{ |
|||
/// <summary>
|
|||
/// Converts a binding value.
|
|||
/// </summary>
|
|||
public interface IValueConverter |
|||
{ |
|||
/// <summary>
|
|||
/// Converts a value.
|
|||
/// </summary>
|
|||
/// <param name="value">The value to convert.</param>
|
|||
/// <param name="targetType">The type of the target.</param>
|
|||
/// <param name="parameter">A user-defined parameter.</param>
|
|||
/// <param name="culture">The culture to use.</param>
|
|||
/// <returns>The converted value.</returns>
|
|||
object Convert(object value, Type targetType, object parameter, CultureInfo culture); |
|||
|
|||
/// <summary>
|
|||
/// Converts a value.
|
|||
/// </summary>
|
|||
/// <param name="value">The value to convert.</param>
|
|||
/// <param name="targetType">The type of the target.</param>
|
|||
/// <param name="parameter">A user-defined parameter.</param>
|
|||
/// <param name="culture">The culture to use.</param>
|
|||
/// <returns>The converted value.</returns>
|
|||
object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture); |
|||
} |
|||
} |
|||
@ -0,0 +1,47 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System.Globalization; |
|||
using Xunit; |
|||
|
|||
namespace Perspex.Markup.UnitTests |
|||
{ |
|||
public class DefaultValueConverterTests |
|||
{ |
|||
[Fact] |
|||
public void Can_Convert_String_To_Int() |
|||
{ |
|||
var result = DefaultValueConverter.Instance.Convert( |
|||
"5", |
|||
typeof(int), |
|||
null, |
|||
CultureInfo.InvariantCulture); |
|||
|
|||
Assert.Equal(5, result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Can_Convert_String_To_Double() |
|||
{ |
|||
var result = DefaultValueConverter.Instance.Convert( |
|||
"5", |
|||
typeof(double), |
|||
null, |
|||
CultureInfo.InvariantCulture); |
|||
|
|||
Assert.Equal(5.0, result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Can_Convert_Double_To_String() |
|||
{ |
|||
var result = DefaultValueConverter.Instance.Convert( |
|||
5.0, |
|||
typeof(string), |
|||
null, |
|||
CultureInfo.InvariantCulture); |
|||
|
|||
Assert.Equal("5", result); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue