Diverse WPF hints

A year ago or so when I started working on WPF I collected bits and bolts in Google’s Notebooks and lost all of it along the way. I found it back in some cache and dump it here in the hope it will be more safe here, for myself or others.
IScrollInfo
Lots of useful methods [...]

WinVista button
A year ago or so when I started working on WPF I collected bits and bolts in Google’s Notebooks and lost all of it along the way. I found it back in some cache and dump it here in the hope it will be more safe here, for myself or others.

IScrollInfo

Lots of useful methods are available directly in the children of the ScrollViewer.

[xml]





[/xml]
which can be controlled in the following way
[csharp]
private void onLoad(object sender, System.EventArgs e)
{
((IScrollInfo)sp1).CanVerticallyScroll = true;
((IScrollInfo)sp1).CanHorizontallyScroll = true;
((IScrollInfo)sp1).ScrollOwner = sv1;
}

private void spLineUp(object sender, RoutedEventArgs e)
{
((IScrollInfo)sp1).LineUp();
}
private void spLineDown(object sender, RoutedEventArgs e)
{
((IScrollInfo)sp1).LineDown();
}
[/csharp]

Binding to enums

I have the following enumeration in the code behind of this sample:
[csharp]
public enum TShirtSizes
{
Small,
Medium,
Large
}
[/csharp]
In the UI of my application, I have a ComboBox and I want the entries in that ComboBox to display the values in the enum.

This is really easy to do. It turns out that there is a static method on Enum that returns an Array of all the values of a particular enumeration. This is how we could get the array of values through code:

Enum.GetValues(typeof(TShirtSizes));

Binding to this method can easily be done in XAML by using ObjectDataProvider:
[xml]







[/xml]

XAML anims in function of events

The interesting bit is here how to animate properties in function of events. The rectangle will resize when the mouse is over it and retract when left.
[xml]

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>


dsfglskjdh flsdjfh slkjdf



[/xml]

Styling the tabcontrol

Try setting the IsEnabled property to ‘False’; the colors will change as defined in the style.
[xml]

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>






Background property information goes here.


Foreground property information goes here.


Border color property information goes here.




[/xml]

Styling the expander

Quite an elaborated styling of the Expander control.
[xml]

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>

CornerRadius="2,0,0,0"
Background="Transparent"
BorderBrush="Black"
BorderThickness="0,0,1,0">

Fill="LightGreen"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M 0 0 L 4 4 L 8 0 Z"/>











This is the text that’s being collapsed/expanded.




[/xml]

Binding the keyboard to ICommand

[csharp]
// Creating CommandBinding and attaching an Executed and CanExecute handler
CommandBinding OpenCmdBinding = new CommandBinding(
ApplicationCommands.Open,
OpenCmdExecuted,
OpenCmdCanExecute);

this.CommandBindings.Add(OpenCmdBinding);

// Creating a KeyBinding between the Open command and Ctrl-R
KeyBinding OpenCmdKeyBinding = new KeyBinding(
ApplicationCommands.Open,
Key.R,
ModifierKeys.Control);
this.InputBindings.Add(OpenCmdKeyBinding);
void OpenCmdExecuted(object target, ExecutedRoutedEventArgs e)
{
MessageBox.Show(“The command has been invoked.”);
}
void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
[/csharp]

which in XAML amounts to
[xml]
< Window.CommandBindings>
Executed="OpenCmdExecuted"
CanExecute="OpenCmdCanExecute"/>


Modifiers="Control"
Key="R" />

[/xml]

Connected rectangles

This example has tow rectangle that you can shift by means of sliders. The important thing to note here is the fact that the line connecting them is driven by binding to the location of the rectangles…I am puzzling a new graph library.

[xml]

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>




DockPanel.Dock="Top" Maximum="1000"
Minimum="10" />



DockPanel.Dock="Top" Maximum="1000"
Minimum="10" />

X2="{Binding ElementName=rec2, Path=(Canvas.Left)}" Y2="{Binding ElementName=rec2, Path=(Canvas.Top)}"
Stroke="Black"
StrokeThickness="1.5"/>
Height="50"
Fill="yellow"
Canvas.Left="{Binding ElementName=slider1, Path=Value}"
Canvas.Top="200"
Name="rec1"
/>
Height="50"
Fill="Red"
Canvas.Top="350"
Name="rec2"
>




[/xml]

Changing the cursor

NameOfElement.Cursor = …

while to change it for the whole application:

Mouse
.OverrideCursor = …

Basic shadows

[xml]

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">



Direction="240" ShadowDepth="5"
Softness=".3" Opacity=".5"/>

[/xml]

Expanding ClassShape

[xml]
This is the animated ClassShape in XAML. I still have to concatenate this in C# into a custom rendered control, add the binding code you’ll find elsewhere in this notebook, some more interactivity and maybe binding to XML data…it all will come together into a smooth and new diagramming library. Netron for .Net 3.0 :)

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>


Viewport="0,0,10,10"
ViewportUnits="Absolute" TileMode="Tile">



















Storyboard.TargetProperty="Opacity"
Duration="0:0:2" From="0.0" To="1.0" RepeatBehavior="Forever" AutoReverse="True"/>
Storyboard.TargetProperty="Height"
Duration="0:0:2" From="50" To="100" RepeatBehavior="Forever" AutoReverse="True"/>



[/xml]

Tagged with:
 

Leave a Reply