Permalink
Browse files

Removed the Behaviors project and implemented Resizing.

  • Loading branch information...
1 parent ea3b08d commit 982149fa12c716f8d1063597438c364112698a79 Thorsten Lorenz committed Jul 31, 2010
@@ -1,8 +0,0 @@
-<Application x:Class="Behaviours.App"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- >
- <Application.Resources>
-
- </Application.Resources>
-</Application>
@@ -1,16 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Configuration;
-using System.Data;
-using System.Linq;
-using System.Windows;
-
-namespace Behaviours
-{
- /// <summary>
- /// Interaction logic for App.xaml
- /// </summary>
- public partial class App : Application
- {
- }
-}
@@ -21,6 +21,10 @@ protected override void OnStartup(StartupEventArgs e)
var mainVM = new MainViewModel(new User { FirstName = "Bob", LastName = "Nerdhead", Email = "Bob@uncool.com" });
var mainWin = new MainView(mainVM);
mainWin.Show();
+
+ var secondVM = new MainViewModel(new User { FirstName = "Bob2", LastName = "Nerdhead2", Email = "Bob@uncool2.com" });
+ var secondWin = new MainView(secondVM);
+ secondWin.Show();
}
}
}
@@ -32,7 +32,10 @@ static void OnValidateChanged(DependencyObject d, DependencyPropertyChangedEvent
_validBorderBrush = txtBox.BorderBrush;
_validBorderThickness = txtBox.BorderThickness;
- txtBox.LostFocus += txtBox_LostFocus;
+ if ((Predicate<string>)e.NewValue != null)
+ txtBox.LostFocus += txtBox_LostFocus;
+ else
+ txtBox.LostFocus -= txtBox_LostFocus;
}
static void txtBox_LostFocus(object sender, RoutedEventArgs e)
View
@@ -1 +1,10 @@
Collection of projects which each shows a simple example for doing somewhat more involved things with WPF.
+ 1. Callbacks Project
+ - demonstrates the use of DependencyProperties that are functions and can be bound to the ViewModel
+ - in this contrived example it is used in order to implement validation
+ 2. Validation Project
+ - demonstrates the declarative validation setup by using the Validating part of the Shared Project
+ 3. Resizing Controls
+ - demonstrates resize behavior implemented as attached property and also by using Interactivity in order to compare both approaches
+
+ Most of the above sample projects make use of tools inside the Shared Project.
@@ -0,0 +1,9 @@
+
+<Application
+ x:Class="ResizingControls.App"
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+ StartupUri="Window1.xaml">
+ <Application.Resources>
+ </Application.Resources>
+</Application>
@@ -0,0 +1,11 @@
+using System.Windows;
+
+namespace ResizingControls
+{
+ /// <summary>
+ /// Interaction logic for App.xaml
+ /// </summary>
+ public partial class App : Application
+ {
+ }
+}
@@ -7,11 +7,11 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
-[assembly: AssemblyTitle("Behaviours")]
+[assembly: AssemblyTitle("ResizingControls")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
-[assembly: AssemblyProduct("Behaviours")]
+[assembly: AssemblyProduct("ResizingControls")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2010")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -5,11 +5,11 @@
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
- <ProjectGuid>{120D1AAB-047B-4043-B712-2843432130F6}</ProjectGuid>
+ <ProjectGuid>{0C7EC9F2-1C2A-46B1-B9A9-D63EF442080F}</ProjectGuid>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
- <RootNamespace>Behaviours</RootNamespace>
- <AssemblyName>Behaviours</AssemblyName>
+ <RootNamespace>ResizingControls</RootNamespace>
+ <AssemblyName>ResizingControls</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
@@ -37,6 +37,10 @@
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
+ <Reference Include="System.Windows.Interactivity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\Lib\Interactivity\System.Windows.Interactivity.dll</HintPath>
+ </Reference>
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
@@ -54,10 +58,18 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
+ <Page Include="Window1.xaml">
+ <Generator>MSBuild:Compile</Generator>
+ <SubType>Designer</SubType>
+ </Page>
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
+ <Compile Include="Window1.xaml.cs">
+ <DependentUpon>Window1.xaml</DependentUpon>
+ <SubType>Code</SubType>
+ </Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs">
@@ -85,8 +97,10 @@
<AppDesigner Include="Properties\" />
</ItemGroup>
<ItemGroup>
- <Folder Include="CustomBehaviors\" />
- <Folder Include="Views\" />
+ <ProjectReference Include="..\Shared\Shared\Shared.csproj">
+ <Project>{AADB75FA-123A-407B-966A-D1121C448136}</Project>
+ <Name>Shared</Name>
+ </ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@@ -0,0 +1,78 @@
+
+<Window
+ x:Class="ResizingControls.Window1"
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+ xmlns:interactive="clr-namespace:Shared.InteractiveBehaviors;assembly=Shared"
+ xmlns:attached="clr-namespace:Shared.AttachedBehaviors;assembly=Shared"
+ xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
+
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+ Width="700"
+ Height="500"
+ Background="Beige"
+ ResizeMode="CanResizeWithGrip"
+ Title="Window1">
+ <Window.Resources>
+ <Style TargetType="{x:Type Label}">
+ <Setter Property="MaxHeight" Value="300"/>
+ <Setter Property="MaxWidth" Value="600"/>
+ <Setter Property="MinHeight" Value="30"/>
+ <Setter Property="MinWidth" Value="100"/>
+ <Setter Property="Width" Value="100" />
+ <Setter Property="Height" Value="50" />
+ <Setter Property="HorizontalAlignment" Value="Center"/>
+ <Setter Property="VerticalAlignment" Value="Center"/>
+ <Setter Property="Background" Value="AntiqueWhite"/>
+ <Setter Property="ContentTemplate">
+ <Setter.Value>
+ <DataTemplate>
+ <Viewbox Stretch="Fill">
+ <TextBlock Text="{TemplateBinding Content}"/>
+ </Viewbox>
+ </DataTemplate>
+ </Setter.Value>
+ </Setter>
+ </Style>
+ <Style x:Key="resizeThumbStyle" TargetType="{x:Type Thumb}">
+ <Setter Property="Width" Value="12"/>
+ <Setter Property="Height" Value="12"/>
+ <Setter Property="Cursor" Value="SizeNWSE"/>
+ <Setter Property="Template">
+ <Setter.Value>
+ <ControlTemplate>
+ <Border Background="Transparent">
+ <Path
+ Data="M0 12 L12 0 M4 12 L12 4 M8 12 L12 8"
+ Opacity="0.5"
+ Stroke="DarkGray"
+ StrokeDashArray="1,2"
+ StrokeThickness="1.6"/>
+ <Border.RenderTransform>
+ <TranslateTransform X="-8" Y="-8"/>
+ </Border.RenderTransform>
+ </Border>
+ </ControlTemplate>
+ </Setter.Value>
+ </Setter>
+ </Style>
+ </Window.Resources>
+ <Grid>
+ <Grid.RowDefinitions>
+ <RowDefinition/>
+ <RowDefinition/>
+ </Grid.RowDefinitions>
+ <GroupBox Grid.Row="0" Header="Attached Behaviors">
+ <Label
+ attached:ResizeAttachedBehavior.IsResizeable="true"
+ attached:ResizeAttachedBehavior.ThumbStyle="{StaticResource resizeThumbStyle}"
+ Content="ResizeMe"/>
+ </GroupBox>
+ <GroupBox Grid.Row="1" Header="Interactive Behaviors">
+ <Label Content="ResizeMe" >
+ <i:Interaction.Behaviors>
+ <interactive:ResizeInteractiveBehavior ThumbStyle="{StaticResource resizeThumbStyle}"/>
+ </i:Interaction.Behaviors>
+ </Label>
+ </GroupBox>
+ </Grid>
+</Window>
@@ -0,0 +1,16 @@
+using System.Windows;
+
+namespace ResizingControls
+{
+ /// <summary>
+ /// Interaction logic for Window1.xaml
+ /// </summary>
+ public partial class Window1 : Window
+ {
+ public Window1()
+ {
+ InitializeComponent();
+
+ }
+ }
+}
@@ -0,0 +1,91 @@
+using System;
+using System.Windows;
+using System.Windows.Controls.Primitives;
+using System.Windows.Documents;
+using System.Windows.Media;
+
+namespace Shared.Adorners
+{
+ public class ResizeAdorner : Adorner
+ {
+ FrameworkElement _adornedElement;
+ Thumb _cornerThumb;
+ VisualCollection _visualChildren;
+
+ public ResizeAdorner(UIElement adornedElement, Style thumbStyle)
+ : base(adornedElement)
+ {
+ _visualChildren = new VisualCollection(this);
+
+ BuildResizeThumb(thumbStyle);
+ _cornerThumb.DragDelta += CornerThumb_DragDelta;
+
+ _adornedElement = AdornedElement as FrameworkElement;
+ InitializeSize(_adornedElement);
+ }
+
+ protected override int VisualChildrenCount
+ {
+ get { return _visualChildren.Count; }
+ }
+
+ protected override Size ArrangeOverride(Size finalSize)
+ {
+ var adornedWidth = _adornedElement.DesiredSize.Width;
+ var adornedHeight = _adornedElement.DesiredSize.Height;
+ var adornerWidth = DesiredSize.Width;
+ var adornerHeight = DesiredSize.Height;
+
+ var rect = new Rect(
+ adornedWidth - adornerWidth / 2,
+ adornedHeight - adornerHeight / 2,
+ adornerWidth,
+ adornerHeight);
+
+ _cornerThumb.Arrange(rect);
+
+ return finalSize;
+ }
+
+ protected override Visual GetVisualChild(int index)
+ {
+ return _visualChildren[index];
+ }
+
+ void BuildResizeThumb(Style thumbStyle)
+ {
+ if (_cornerThumb == null)
+ {
+ _cornerThumb = new Thumb { Style = thumbStyle };
+ _visualChildren.Add(_cornerThumb);
+ }
+ }
+
+ void CornerThumb_DragDelta(object sender, DragDeltaEventArgs e)
+ {
+ var thumb = sender as Thumb;
+
+ if (_adornedElement != null && thumb != null)
+ {
+
+ _adornedElement.Width = Math.Max(_adornedElement.Width + e.HorizontalChange, thumb.DesiredSize.Width);
+ _adornedElement.Height = Math.Max(_adornedElement.Height + e.VerticalChange, thumb.DesiredSize.Height);
+ }
+ }
+
+ void InitializeSize(FrameworkElement adornedElement)
+ {
+ if (adornedElement.Width.Equals(Double.NaN))
+ adornedElement.Width = adornedElement.DesiredSize.Width;
+ if (adornedElement.Height.Equals(Double.NaN))
+ adornedElement.Height = adornedElement.DesiredSize.Height;
+
+ FrameworkElement parent = adornedElement.Parent as FrameworkElement;
+ if (parent != null)
+ {
+ adornedElement.MaxHeight = parent.ActualHeight;
+ adornedElement.MaxWidth = parent.ActualWidth;
+ }
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 982149f

Please sign in to comment.