Adding .cs to ResourceDictionary?

I have a DataTemplate in a dictionnary ressource, and in some I need a button and I don’t know how I can use the code to manage events.

I tried putting the class in my dictionnary resource as follows:

<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 defined the class in the cs file as follows:

namespace SLProject.Templates
{
    partial class TVTemplate
    { 

    }
}

The line is fine, but when the application starts, I get a XAML error following:

AG_E_PARSER_BAD_TYPE

I tried everything that I knew how to change a class in ClassModifier, make the class an inherited class RessourceDictionnary ... no.

Someone has an ideal ...

Thank.

+5
source share
4 answers

x:Class, ResourceDictionary. (.. x:Class="WpfApplication.MyClass"), partial ( VS 2010 ).

:

1. WPF (WpfApplication)

2. (TestClass.cs)

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. ResourceDictionary (Resources.xaml),

<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. , MainWindow.xaml

<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>

Style, , ResourceDictionary.

+6

x: Class, , . , :

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

I checked, and this is just a mistake copying the past. I defined the class well once.

0
source

It would be best to create your own user control and add your own events to it. and then put all this user control in the resource dictionary.

0
source

All Articles