Skip to content

Commit

Permalink
Deferring to OnLoaded.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLarsson committed Jun 24, 2015
1 parent 81abbf0 commit 07a16fa
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 63 deletions.
80 changes: 58 additions & 22 deletions Gu.Wpf.Validation.Demo/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
xmlns:demo="clr-namespace:Gu.Wpf.Validation.Demo"
xmlns:validation="http://gu.se/Validation"
xmlns:wpf="clr-namespace:Gu.Wpf.Validation.Demo.Wpf"
StartupUri="MainWindow.xaml">
StartupUri="MainWindow.xaml"
>
<Application.Resources>
<demo:NullConverter x:Key="NullConverter" />
<converters:TextBoxChildConverter x:Key="TextBoxChildConverter" />
Expand All @@ -25,7 +26,9 @@
<StreamGeometry x:Key="FolderEllipsisGeometry">F1 M 21,30L 56,30L 56,50L 21,50L 21,30 Z M 52,28L 37,28C 38,25 39,24 39,24L 51,24C 51,24 52,25 52,25L 52,28 Z M 54,52C 55,52 56,53 56,55C 56,56 55,57 54,57C 52,57 51,56 51,55C 51,53 52,52 54,52 Z M 47,52C 48,52 49,53 49,55C 49,56 48,57 47,57C 45,57 44,56 44,55C 44,53 45,52 47,52 Z M 40,52C 41,52 42,53 42,55C 42,56 41,57 40,57C 38,57 37,56 37,55C 37,53 38,52 40,52 Z</StreamGeometry>
<StreamGeometry x:Key="PlayGeometry">F1 M 30.0833,22.1667L 50.6665,37.6043L 50.6665,38.7918L 30.0833,53.8333L 30.0833,22.1667 Z</StreamGeometry>

<Style x:Key="HeaderStyle" TargetType="{x:Type TextBlock}">
<Style x:Key="HeaderStyle"
TargetType="{x:Type TextBlock}"
>
<Setter Property="FontSize" Value="24" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="VerticalAlignment" Value="Center" />
Expand All @@ -38,76 +41,98 @@
<wpf:ValidationTemplateSelector x:Key="ValidationTemplateSelector">
<wpf:ValidationTemplateSelector.NoErrorTemplate>
<DataTemplate>
<Path Data="{StaticResource CheckMarkGeometry}" Fill="GreenYellow" />
<Path Data="{StaticResource CheckMarkGeometry}"
Fill="GreenYellow"
/>
</DataTemplate>
</wpf:ValidationTemplateSelector.NoErrorTemplate>

<wpf:ValidationTemplateSelector.MissingTemplate>
<DataTemplate>
<Path Data="{StaticResource ChevronLeftGeometry}" Fill="LightBlue" />
<Path Data="{StaticResource ChevronLeftGeometry}"
Fill="LightBlue"
/>
</DataTemplate>
</wpf:ValidationTemplateSelector.MissingTemplate>

<wpf:ValidationTemplateSelector.ErrorsTemplate>
<DataTemplate>
<Path Data="{StaticResource CrossGeometry}" Fill="Red" />
<Path Data="{StaticResource CrossGeometry}"
Fill="Red"
/>
</DataTemplate>
</wpf:ValidationTemplateSelector.ErrorsTemplate>
</wpf:ValidationTemplateSelector>

<Style x:Key="HeaderedSettingStyle" TargetType="{x:Type HeaderedContentControl}">
<Style x:Key="HeaderedSettingStyle"
TargetType="{x:Type HeaderedContentControl}"
>
<Setter Property="Margin" Value="8,8" />
<Setter Property="Focusable" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type HeaderedContentControl}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="{x:Static wpf:SharedSizeGroups.LabelColumn}" />
<ColumnDefinition Width="Auto"
SharedSizeGroup="{x:Static wpf:SharedSizeGroups.LabelColumn}"
/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="0" ContentSource="Header" />
<ContentPresenter Grid.Column="0"
ContentSource="Header"
/>

<ContentPresenter Grid.Column="1"
Margin="8,0"
ContentSource="Content" />
ContentSource="Content"
/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock VerticalAlignment="Center" Text="{Binding}" />
<TextBlock VerticalAlignment="Center"
Text="{Binding}"
/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>

<Style x:Key="SymbolHeaderedSettingStyle"
BasedOn="{StaticResource HeaderedSettingStyle}"
TargetType="{x:Type HeaderedContentControl}">
TargetType="{x:Type HeaderedContentControl}"
>
<Setter Property="validation:Scope.IsValidationScope" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type HeaderedContentControl}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="{x:Static wpf:SharedSizeGroups.LabelColumn}" />
<ColumnDefinition Width="Auto"
SharedSizeGroup="{x:Static wpf:SharedSizeGroups.LabelColumn}"
/>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="0" ContentSource="Header" />
<ContentPresenter Grid.Column="0"
ContentSource="Header"
/>

<ContentPresenter x:Name="ValuePresenter"
Grid.Column="1"
Margin="8,0"
ContentSource="Content" />
ContentSource="Content"
/>

<ContentPresenter Grid.Column="2"
Content="{Binding (validation:Scope.Errors),
RelativeSource={RelativeSource TemplatedParent}}"
ContentTemplateSelector="{StaticResource ValidationTemplateSelector}" />
ContentTemplateSelector="{StaticResource ValidationTemplateSelector}"
/>
</Grid>
</ControlTemplate>
</Setter.Value>
Expand All @@ -116,13 +141,18 @@

<Style x:Key="TightHeaderedSettingStyle"
BasedOn="{StaticResource HeaderedSettingStyle}"
TargetType="{x:Type HeaderedContentControl}">
TargetType="{x:Type HeaderedContentControl}"
>
<Setter Property="Margin" Value="0,0" />
</Style>

<Style x:Key="SettingsListStyle" TargetType="{x:Type ItemsControl}">
<Style x:Key="SettingsListStyle"
TargetType="{x:Type ItemsControl}"
>
<Style.Resources>
<Style BasedOn="{StaticResource TightHeaderedSettingStyle}" TargetType="{x:Type HeaderedContentControl}" />
<Style BasedOn="{StaticResource TightHeaderedSettingStyle}"
TargetType="{x:Type HeaderedContentControl}"
/>
</Style.Resources>
<Setter Property="Grid.IsSharedSizeScope" Value="True" />
<Setter Property="KeyboardNavigation.TabNavigation" Value="Cycle" />
Expand All @@ -137,14 +167,17 @@
</ControlTemplate.Resources>-->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="{x:Static wpf:SharedSizeGroups.LabelColumn}" />
<ColumnDefinition Width="Auto"
SharedSizeGroup="{x:Static wpf:SharedSizeGroups.LabelColumn}"
/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="2"
Grid.IsSharedSizeScope="True"
IsItemsHost="True" />
IsItemsHost="True"
/>
</Grid>
</ControlTemplate>
</Setter.Value>
Expand All @@ -153,9 +186,12 @@

<Style x:Key="TightSettingsListStyle"
BasedOn="{StaticResource SettingsListStyle}"
TargetType="{x:Type ItemsControl}">
TargetType="{x:Type ItemsControl}"
>
<Style.Resources>
<Style BasedOn="{StaticResource TightHeaderedSettingStyle}" TargetType="{x:Type HeaderedContentControl}" />
<Style BasedOn="{StaticResource TightHeaderedSettingStyle}"
TargetType="{x:Type HeaderedContentControl}"
/>
</Style.Resources>
</Style>
</Application.Resources>
Expand Down
1 change: 0 additions & 1 deletion Gu.Wpf.Validation.Demo/Wpf/ValidationTemplateSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public override DataTemplate SelectTemplate(object item, DependencyObject contai
return MissingTemplate;
}
return ErrorsTemplate;
return base.SelectTemplate(item, container);
}
}
}
3 changes: 3 additions & 0 deletions Gu.Wpf.Validation.Tests/Gu.Wpf.Validation.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
<Name>Gu.Wpf.Validation</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Sandbox\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
6 changes: 6 additions & 0 deletions Gu.Wpf.Validation.Tests/Helpers/TextBoxTestExt.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Gu.Wpf.Validation.Tests.Helpers
{
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

Expand All @@ -13,5 +14,10 @@ public static void WriteText(this TextBox textBox, string text, bool overwrite =
}
TextCompositionManager.StartComposition(new TextComposition(InputManager.Current, textBox, text));
}

public static void RaiseLoadedEvent(this TextBox textBox)
{
textBox.RaiseEvent(new RoutedEventArgs(FrameworkElement.LoadedEvent));
}
}
}
15 changes: 10 additions & 5 deletions Gu.Wpf.Validation.Tests/InputTests/RawValuesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void UpdatesWhenVmChanges()
Mode = BindingMode.TwoWay
};
BindingOperations.SetBinding(textBox, Input.ValueProperty, binding);
textBox.RaiseLoadedEvent();
Assert.IsNullOrEmpty(textBox.GetRawText());
Assert.AreEqual(null, textBox.GetRawValue());

Expand All @@ -52,21 +53,21 @@ public void UpdatesOnUserInput()
Mode = BindingMode.TwoWay
};
BindingOperations.SetBinding(textBox, Input.ValueProperty, binding);
textBox.RaiseLoadedEvent();

Assert.IsNullOrEmpty(textBox.GetRawText());
Assert.AreEqual(null, textBox.GetRawValue());

textBox.WriteText("1.234");

Assert.AreEqual("1.23", textBox.Text);
Assert.AreEqual("1.234", textBox.GetRawText());
Assert.AreEqual(1.234, textBox.GetRawValue());
Assert.AreEqual(RawValueSource.User, textBox.GetRawValueSource());

TextCompositionManager.StartComposition(new TextComposition(InputManager.Current, textBox, "56"));
textBox.WriteText("56", false);

Assert.AreEqual("1.24", textBox.Text);
Assert.AreEqual("1.2356", textBox.GetRawText());
Assert.AreEqual(1.2356, textBox.GetRawValue());
Assert.AreEqual("1.23456", textBox.GetRawText());
Assert.AreEqual(1.23456, textBox.GetRawValue());
Assert.AreEqual(RawValueSource.User, textBox.GetRawValueSource());
}

Expand All @@ -84,6 +85,8 @@ public void UpdatesOnUserErrorInput()
Mode = BindingMode.TwoWay
};
BindingOperations.SetBinding(textBox, Input.ValueProperty, binding);
textBox.RaiseLoadedEvent();

Assert.IsNullOrEmpty(textBox.GetRawText());
Assert.AreEqual(null, textBox.GetRawValue());

Expand All @@ -108,6 +111,8 @@ public void UpdatesRawValueOnCultureChange()
Mode = BindingMode.TwoWay
};
BindingOperations.SetBinding(textBox, Input.ValueProperty, binding);
textBox.RaiseLoadedEvent();

Assert.IsNullOrEmpty(textBox.GetRawText());
Assert.AreEqual(null, textBox.GetRawValue());

Expand Down
5 changes: 4 additions & 1 deletion Gu.Wpf.Validation.Tests/InputTests/SourceValueTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void WhenDataContextChanges()
Mode = BindingMode.TwoWay
};
BindingOperations.SetBinding(textBox, Input.ValueProperty, binding);
textBox.RaiseEvent(new RoutedEventArgs(FrameworkElement.LoadedEvent));
textBox.RaiseLoadedEvent();
Assert.AreEqual(null, textBox.GetSourceValueType());

var vm = new DummyViewModel { NullableDoubleValue = null };
Expand All @@ -39,6 +39,7 @@ public void NullableDouble()
Mode = BindingMode.TwoWay
};
BindingOperations.SetBinding(textBox, Input.ValueProperty, binding);
textBox.RaiseLoadedEvent();
textBox.RaiseEvent(new RoutedEventArgs(FrameworkElement.LoadedEvent));
Assert.AreEqual(typeof(double?), textBox.GetSourceValueType());

Expand All @@ -58,6 +59,7 @@ public void WithConverter()
Mode = BindingMode.TwoWay
};
BindingOperations.SetBinding(textBox, Input.ValueProperty, binding);
textBox.RaiseLoadedEvent();
Assert.AreEqual(typeof(int), textBox.GetSourceValueType());
}

Expand All @@ -68,6 +70,7 @@ public void SideEffectsOnChange()
Assert.IsNull(BindingOperations.GetBindingExpression(textBox, System.Windows.Controls.TextBox.TextProperty));

textBox.SetSourceValueType(typeof(int));
textBox.RaiseLoadedEvent();
Assert.NotNull(BindingOperations.GetBindingExpression(textBox, System.Windows.Controls.TextBox.TextProperty));

Assert.AreEqual(DefaultNumberStyles.DefaultInteger, textBox.GetNumberStyles());
Expand Down
Loading

0 comments on commit 07a16fa

Please sign in to comment.