Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding .cs in a ResourceDictionary?

I have DataTemplate in a ressource dictionnary, and in some, I need button and i don't know how i can use code behind for manage events.

I tried to put a class in my resource dictionnary like that :

<ResourceDictionary 
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   x:Class="SLProject.Templates"
   x:Class="TVTemplate">

And I definied the class in the cs file like that :

namespace SLProject.Templates
{
    partial class TVTemplate
    { 

    }
}

The build is OK but when the application started, I obtains XAML error following :

AG_E_PARSER_BAD_TYPE

I tried all I know like change the class kind to a ClassModifier, make the class to an inherited class of RessourceDictionnary ... no way.

Someone have an idee ...

Thanks.

like image 359
gtoulouse Avatar asked Mar 01 '10 15:03

gtoulouse


People also ask

How do you add a ResourceDictionary?

We can define the styles in WPF XAML files, or perhaps we can manage to accumulate all our useful styles for a particular application in a resource dictionary file. Adding a resource dictionary is pretty simple. We have to select the project or folder in Solution Explorer and then right click and select “Add”.

How do I add a resource to a project in WPF?

To add a Resource Dictionary into your WPF application, right click the WPF project > add a Resource Dictionary. Now apply the resource "myAnotherBackgroundColor" to button background and observe the changes.

What is ResourceDictionary WPF?

In Extensible Application Markup Language (XAML), the ResourceDictionary class is typically an implicit collection element that is the object element value of several Resources properties, when given in property element syntax. For details on implicit collections in XAML, see XAML Syntax Terminology.

What is CLR namespace?

clr-namespace: The CLR namespace declared within the assembly that contains the public types to expose as elements. assembly= The assembly that contains some or all of the referenced CLR namespace. This value is typically just the name of the assembly, not the path, and does not include the extension (such as .


1 Answers

Using the x:Class attribute allows you to define a codebehind for a ResourceDictionary. You must specify the complete namespace of the class (i.e. x:Class="WpfApplication.MyClass"), and such class has to be defined as partial (at least VS 2010 complains and does not compile without such modifier).

I mocked-up a simple example:

1. Create a new WPF application project (WpfApplication)

2. Add a new class file (TestClass.cs) and paste the following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;

namespace WpfApplication
{
    public partial class TestClass
    {
        private void OnDoubleClick(object obj, MouseButtonEventArgs args)
        {
            MessageBox.Show("Double clicked!");
        }
    }
}

3. Add a new ResourceDictionary (Resources.xaml), open the file and paste the following code

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    x:Class="WpfApplication.TestClass">
    <Style TargetType="{x:Type Label}">
        <EventSetter Event="Label.MouseDoubleClick" Handler="OnDoubleClick"/>
    </Style>
</ResourceDictionary>

4. Finally, open the MainWindow.xaml and past the following code

<Window x:Class="WpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary Source="Resources.xaml"/>
    </Window.Resources>
    <Grid>
        <Label Content="Double click here..." HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Background="Red"/>
    </Grid>
</Window>

In the example I wire-up a double-click event from a Style, since it is a scenario requiring you to call some code from a ResourceDictionary.

like image 151
BladeWise Avatar answered Sep 28 '22 12:09

BladeWise