CodeCopy

October 13, 2009

WCF selfhosting helper

Filed under: c#,wpf — sterndorff @ 16:28
Tags: ,

A simple wrapper for hosting wcf services that works with both service references and web references.

WcfServiceHelper.Instance.StartService<MyService, IMyService>(new ServerConf());

More can be done in terms of configuration and a StopService method is missing. That is left for the reader (or untill I need it in my app).

(more…)

October 6, 2009

WPF Databinding Enum with nice descriptions

Filed under: c#,wpf — sterndorff @ 17:28
Tags: , , ,

I have an Enum that I want to databind to a wpf drop down box. But I want to represent the enum with some nice descriptions instead of Enum.ToString().

1) The enum with the nice descriptions: MyEnum.cs

    public enum MyEnum
    {
        [Description("Default value")] // Default value if enum is nullable and used in databinding
        DefaultValue,
        [Description("Value1 description")]
        Value1,
        [Description("Value2 description")]
        Value2
    }

(more…)

September 18, 2009

“Casting” list to observablecollection

Filed under: c#,wpf — sterndorff @ 11:04
Tags: , , ,

Easy as pie without for loops:

List<string> myList = new List<string>();
ObservableCollection<string> myObs = new ObservableCollection<string>();

myList.ForEach(i => myObs.Add(i));

September 16, 2009

Wpf UIElement keyboard focus helper

Filed under: c#,wpf — sterndorff @ 10:15
Tags: , ,

Ever had problems setting focus on a wfp control? I’ve found a small and ugly way to help. Using this extention method helps in a lot of scenarios:


public static class FocusHelper
{
    public static void DelayedFocus(this UIElement uiElement)
    {
        uiElement.Dispatcher.BeginInvoke(
        new Action(delegate
        {
            uiElement.Focusable = true;
            uiElement.Focus();
            Keyboard.Focus(uiElement);
        }),
        DispatcherPriority.Render);
    }
}

And yes I know it’s not pretty. Longer discussion here: http://apocryph.org/2006/09/10/wtf_is_wrong_with_wpf_focus/

September 15, 2009

Putting wfp styles in seperate resource files

Filed under: c#,wpf — sterndorff @ 09:17
Tags: , , ,

First define a seperate resource file. In this example /Templates/DefaultStyles.xaml:

<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <style x:Key="TextblockHeader">
        <setter Property="Control.FontWeight" Value="Bold"/>
        <setter Property="Control.FontSize" Value="14"/>
        <setter Property="Control.Margin" Value="10,5,10,5"/>
    </style>
</resourcedictionary>

The hard part is including this resource in your app/usercontrol.
(more…)

September 10, 2009

Implicit Two Way binding

Filed under: c#,wpf — mazzoo @ 19:12
Tags: , , ,

If you try to use TwoWay binding directly on for example a textbox in a listbox. You will have a problem.

<ListBox Grid.Column="1" Grid.Row="1" ItemsSource="{Binding ListOfStrings}">
   <ListBox.ItemTemplate>
       <DataTemplate>
          <TextBox Text="{Binding}"/>
       </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

The result you want is obvious, you want to allow the TextBox manipulate the ListOfStrings ( oftype List<string> ).

The error you will get will look something like: Two-way binding requires Path or XPath.

The solution is this, it’s not pretty but it works !

<ListBox Grid.Column="1" Grid.Row="1" ItemsSource="{Binding ListOfStrings}">
   <ListBox.ItemTemplate>
      <DataTemplate>
         <TextBox Text="{Binding Path=DataContext, RelativeSource={RelativeSource Self}}"/>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.