Using Merged Resource Dictionaries inside Themes/generic.xaml

December 20, 2007

For some reason including a ResourceDictionary into the Themes/generic.xaml does not behave the same as including a ResourceDictionary into App.xaml.

If you include a ResouceDictionary into Themes/generic.xaml you will receive an exception telling you that the dictionary cannot be assigned to property ‘Source’ of object, yada, yada, yada, very informative.

Exception:’MyDictionary.xaml’ value cannot be assigned to property ‘Source’ of object ‘System.Windows.ResourceDictionary’. Cannot loc
ate resource ‘mydictionary.xaml’.  Error at object ‘System.Windows.ResourceDictionary’, Line 9 Position 23.

What the exception is trying to tell you is that the included ResourceDictionary file was not found on the output folder, (meaning that it was expected to be an external resource).

What to do then, instead of:

<ResourceDictionary.MergedDictionaries>

    <ResourceDictionary Source=”MyDictionary.xaml” />

</ResourceDictionary.MergedDictionaries>

You should write it like this (specifying that the included resource is embedded into the assembly):

<ResourceDictionary.MergedDictionaries>

    <ResourceDictionary Source=”/My.Controls;component/themes/MyDictionary.xaml” />

</ResourceDictionary.MergedDictionaries>


RadioButton IsChecked Binding Problem in .NET 3.5 RTM

November 22, 2007

I have written two posts in the past about a bug in WPF’s RadioButton IsChecked property change notification.

On my first post I described the problem and in second post I proposed and demonstrated a workaround to the problem.

Now after the release of the .NET Frameworks 3.5, I checked it again and hoping that it was fixed, but unfortunately it wasn’t fixed 8O . But the same workarounds that I described in my previous post in this issue still do the jobs.


Pistachio

November 12, 2007

Grant Hinkson from Infragistics created Pistachio, a resource visualizer utility, which identifies all resources defined within the project and show you which resources are used and where they are used.

It’s a helpful tool to clean up unused resources and to understand the current resource structure, especially when working with large WPF projects where resources are extensible use.

It’s free to download at the moment and worth to try it!

                           


Embedding Bitmaps inside XAML

October 8, 2007

I needed a type of BitmapSource that allows me to embed an image inside a XAML file. WPF has a number of derived BitmapSource classes, each one for its specialized task, one specific, BitmapImage, allows you to load images using XAML, but BitmapImage refer to external images in XAML files and does not embed images in a XAML file.

So I decided to create my own class to achieve the task.

What did I do?

  1. A BitmapSource derived class that expose a property with the bitmap info and can act as a reference to another BitmapSource.
  2. A container struct, BitmapInfo, to hold all the information needed to reconstruct the image, pixel array, size, format, etc.
  3. A TypeConverter that convert BitmapInfo to a serialized byte64 string and vice versa.

Using the EmbeddedBitmapSource is seamless as using any other derived BitmapSource class. For example:

Image myImage = new Image();

BitmapImage bmpImage = new BitmapImage( new Uri( “someimage.jpg” ) );

myImage.Source = new EmbeddedBitmapSource( bmpImage );

I included the sources with a sample application that let you save the image into a XAML file (the XAML file can be found after saving where the assembly is) and later to load it.

EmbeddedSample

On the upper right side is the original image and on the lower part of the window is the embedded image that was loaded from the XAML file.

Source code (VS2008 project) can be downloaded from here (Remember to change the .DOC extension to .ZIP)

By the way, I took the picture some year ago in Chile near Puerto Tranquilo. In Lake General Carrera are marble caves formations, called the Capilla de marmol and Catedral de marmol that were carved by water over time. One of those from a specific angle a dog’s head can be seen.


RadioButton IsChecked Binding Problem – Conclusions

October 2, 2007

After I posted my previous post on this subject, I done some research on this issue and I found the following:

  1. The binding between the IsChecked property and a property in the “code behind” works in only if the binding is declared inside a Control Template, see code below.
  2. When binding between xaml elements the problem does not occur.

<UserControl

    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation

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

    x:Class=RadioButtonTest.TemplatedControl

    Height=300 Width=300 Template={DynamicResource UserControlControlTemplate1}>

  <UserControl.Resources>

    <BooleanToVisibilityConverter x:Key=BooleanToVisibilityConverter/>

    <ControlTemplate x:Key=UserControlControlTemplate1 TargetType={x:Type UserControl}>

      <Grid>

        <StackPanel VerticalAlignment=Top>

          <StackPanel VerticalAlignment=Top>

 

            <RadioButton

                Height=16

                HorizontalAlignment=Left

                Name=isEllipse

 

                IsChecked={Binding Path=IsEllipse,

                                        RelativeSource={RelativeSource TemplatedParent},

                                        Mode=TwoWay,

                                        UpdateSourceTrigger=PropertyChanged}

                Content=Ellipse/>

 

            <RadioButton

                Height=16

                HorizontalAlignment=Left

                Name=isRectangle

                Content=Rectangle/>

 

          </StackPanel>

          <TextBlock Text=Binding Visibilty to Radio Button IsChecked />

          <TextBlock Text=property with BooleanToVisibilityConverter:/>

          <Ellipse x:Name=EllipseTip Fill=#FF000000

                  Width=30 Height=30  StrokeThickness=1

                  Visibility={Binding Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=isEllipse, Mode=Default}/>

          <Rectangle x:Name=RectangleTip Fill=#FF000000

                    Width=30 Height=30 StrokeThickness=1

                      Visibility={Binding Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=isRectangle, Mode=Default}/>

          <TextBlock Text=Binding to SelectionText Property />

          <TextBlock Text=and IsEnabled to rectangle radio button:/>

          <TextBox Text={Binding Path=SelectionText, RelativeSource={RelativeSource TemplatedParent}, Mode=Default}

          IsEnabled={Binding Path=IsChecked, ElementName=isRectangle, Mode=Default}/>

        </StackPanel>

      </Grid>

    </ControlTemplate>

  </UserControl.Resources>

</UserControl>

I did a sample which uses two similar controls with the difference that one uses a control template and the other do not, and demonstrates the differences while binding to properties on the UserControls (TemplatedControl and NoTemplateControl).

radioBtnTest

You can download a sample code (VS2008 Beta2 project) here, Remember to change the .DOC extension to .ZIP.