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="TVTe开发者_C百科mplate">
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.
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
.
You have the x:Class attribute defined twice, which is why you're getting the parser error. Change your declaration to this and it should work:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SLProject.Templates.TVTemplate">
I Checked, and it's just an error of copy-past. I have well the class definied one time.
Best thing would be to make your own usercontrol and add your events in it . and later put this entire usercontrol in resource dictionary.
精彩评论