Showing posts with label XAML. Show all posts
Showing posts with label XAML. Show all posts

Thursday, July 30, 2009

ComboBox In Silverlight 3 And Binding

While recent exploration of Silverlight 3 I've stuck with the problem with following code:

<ComboBox SelectedItem="{Binding Path=Region}" ItemsSource="{Binding Path=Region.ParentCollection}" DisplayMemberPath="Name" />

It's not a good piece of code from design point of view: city references region in which it is located and region has a reference to collection of all available regions. And the problem with that code is that combo box shows list of regions but it doesn't select region in the box. Setting SelectedIndex to constant value also cause ArgumentOutOfRangeException. After several trials and errors I've found that if I firstly assign ItemsSource attribute in XAML and only then SelectedItem ComboBox works correctly! It seems that order of attributes in Silverlight 3 XAML matters. I've never seen that order of attributes may be important. As I know XSD doesn't allow to define strict order of attributes (but this can be done for the elements), so this looks very ridiculous.

Or may be my tricky and ugly code was the cause of such strange behaviour of framework? Hope in future I'll found answer to this question.

Friday, December 26, 2008

WPF Application Doesn't Load Resources in IDE

While working with WPF application I've experienced a problem: library assembly requests resource from System.Windows.Application which is defined in executable assembly in XAML file and this resource isn't loaded while I work in IDE! If I debug application all works fine, but XAML designer doesn't load resource, library method fails and window isn't rendered in designer. Of course I still can work with pure XAML and mostly I do so, but I prefer to have an ability to quickly watch results of my work in designer. So to workaround this problem I changed failed method so he will return not fully correct value which will not depend on application resources.

Monday, August 25, 2008

Enum as a ComboBox.ItemSource in WPF

Currently I'm working on my first project with WPF and study it on a practice. In this short article I want to give example how in XAML make ComboBox list values (or names) of enum.

ItemsControl.ItemsSource uses IEnumerable as source of items to list in ItemsControl (which ComboBox is derived from). In C# you can do this very simply:

comboBox.ItemsSource = Enum.GetNames( typeof( Dock ) );

You can do the same using only XAML:

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="ItemsSource test" Height="70" Width="150" WindowStyle="ToolWindow">
<Window.Resources>
<ObjectDataProvider x:Key="DockNames"
MethodName="GetNames" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type Type="{x:Type Dock}" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid>
<ComboBox Margin="5" Width="120" Height="23" Name="comboBox"
HorizontalAlignment="Left" VerticalAlignment="Top"
ItemsSource="{Binding Source={StaticResource DockNames}}"
SelectedIndex="0">
</ComboBox>
</Grid>
</Window>

In this sample ObjectDataProvider is a wrapper for a method call and to assign this data provider to ItemsSource binding is used. As you can see XAML version of requires more lines of code. But it is usefull if development team strictly follows the rule that interface view must be defined in XAML without code on C#.