Jul 29 2009

How to set the windows position in WPF

Category: WPF | C#fossmo @ 17:18

To position a window manually on the screen in Window Presentation Foundation you can use these system properties Window.Left and Window.Top. To get the size of the screen you can use these system properties:

System.Windows.SystemParameters.PrimaryScreenWidth;
System.Windows.SystemParameters.PrimaryScreenHeight;

To set a window (the green one) position like the one shown at the image below, you can use this method:

private void SetWindowPosition()
{
    Left = SystemParameters.PrimaryScreenWidth - (double)GetValue(WidthProperty) - 30; ;
    Top = 150;
}

image

Tags:

Currently rated 4.5 by 2 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Jul 5 2009

Converting from SVG to XAML

Category: WPF | Softwarefossmo @ 19:22

To night I spent hours trying to convert from a SVG file to XAML. The reason for wanting to do the converting, is that I want a vector based image in a WPF application I’m currently developing. Using a vector based image will scale much better than using a bitmap image.

imageThe solution

I first downloaded a vector based image from this url: http://iconeden.com/icon/free/get/milky-a-free-vector-iconset
Inside the zip file you will find a SVG file. You can open this file using Adobe Illustrator, but I prefer using free alternatives, so I downloaded Inkscape. Inkscape is a open source vector graphics editor. I opened the file, and it contained several images. I wanted one of them; a clock. I copied and pasted the image into a new instance of Inkscape. If you choose Save as from the file menu in Inkscape, you will notice that I gives you the option to save as XAML. I tried doing that without success. It turned out that the XAML produced by Inkscape is not valid when opening it in Expression Blend or Visual Studio. So I saved the file as Clock.svg

On Codeplex there’s a project called XamlTune. I downloaded it and used the command prompt tool called svg2xaml.exe found in the package. The output from this small application were a XAML file called Clock.xaml. I opened this file in Visual Studio and guess what? It worked. The image were converted from SVG to XAML without any errors.

If you need to convert from SVG to XAML, I recommend that you look at this tool. I have only used it a few time, but it looks good so fare.

Tags:

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Mar 18 2009

Speaking at MSDN Live in Oslo tomorrow

Category: Presentation | WPFfossmo @ 02:14

This last mount I have been around cities in Norway giving a presentation called ‘WPF Done Right’. This is in relation to Microsofts MSDN Live tour where they go to 4 large cities in Norway with presentations about new and upcoming technologies. The presentation I’m giving is about Composite Application Guidance. You may know it as Prism because this was the codename they used when they started developing this guidance. It’s important to distinguish Prism from Composite Application Block. Prism equals freedom. You can do whatever you want with this framework/guidelines. If you want to use something else than what comes out-of-the-box, you can. A god example of this is Unity. Unity is a IOC container made by Microsoft. If you don’t want to use it, you don’t have to. You can switch to Autofact or Windsor. It’s up to you. In CAB you have to follow all the rules that the CAB team have decided. CAB equals not so much freedom.

I’m really excited about this framework and I love working with it. I will definitely write posts about this framework in the future. 
The picture below is from the presentation in Trondheim.

image

Rune Grothaug at Microsoft took some pictures from the event in Trondheim. You can view them here.

I’m giving this presentation together with Kjetil Klaussen, a friend and colleague from Acando.

Tags: ,

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Mar 17 2009

How to create an attached property in WPF using a ComboBox

Category: WPF | C#fossmo @ 17:37

This is mostly a note to myself on how to create an attached property in WPF using a ComboBox.

public class SelectionBehavior
{
    public static DependencyProperty SelectionChangedProperty =
            DependencyProperty.RegisterAttached("SelectionChanged",
            typeof(ICommand),
            typeof(SelectionBehavior),
            new UIPropertyMetadata(SelectionBehavior.SelectedItemChanged));
    public static void SetSelectionChanged(DependencyObject target, ICommand value)
    {
        target.SetValue(SelectionBehavior.SelectionChangedProperty, value);
    }
    private static void SelectedItemChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
    {
        Selector element = target as Selector;
        if (element == null) throw new InvalidOperationException("This behavior can be attached to Selector item only.");
        if ((e.NewValue != null) && (e.OldValue == null))
        {
            element.SelectionChanged += SelectionChanged;
        }
        else if ((e.NewValue == null) && (e.OldValue != null))
        {
            element.SelectionChanged -= SelectionChanged;
        }
    }
    private static void SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        UIElement element = (UIElement)sender;
        ICommand command = (ICommand)element.GetValue(SelectionBehavior.SelectionChangedProperty);
        command.Execute(((Selector) sender).SelectedValue);
    }
}

And in the XAML file add

<ComboBox local:SelectionBehavior.SelectionChanged="{Binding MyCommand}"  
              ItemsSource="{Binding MySource}"
              Name="MyComboBox"/> 

 

MyCommand can be a DelegateCommand from CAG.

Tags:

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Dec 12 2008

Composite WPF

Category: Links | WPFfossmo @ 09:48

Composite WPF formerly known as Prism, are guidelines for how to setup the architecture when creating WPF applications. I have gathered some links to resources for learning about this stuff. Please give me feedback if there are other resources I should look into. Anyway, here are the links:

Podcast/Screencasts:

Glenn Block and Brian Noyes:
http://www.dotnetrocks.com/default.aspx?showNum=374

Brian Noyes on Prism
http://www.dnrtv.com/default.aspx?showNum=124

Documentation/Articles:

Composite WPF and Silverlight on CodePlex
http://www.codeplex.com/CompositeWPF

Composite Application Guidance for WPF on MSDN
http://msdn.microsoft.com/en-us/library/cc707819.aspx

MSDN Magazine: Patterns for Building Composite Applications with WPF
http://msdn.microsoft.com/en-us/magazine/cc785479.aspx

Blogs:

Glenn Block’s Blog:
http://blogs.msdn.com/gblock/archive/tags/prism/default.aspx

Blaine Wastell’s Blog:
http://blogs.msdn.com/blaine/archive/tags/Prism/default.aspx

Brian Noyes’ Blog
http://www.softinsight.com/bnoyes/

Tags:

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5