WPF code-behind for resources? - wpf

I'm working on a wpf application, and up until recently, I had a ResourceDictionary inside my main window's resources part of the xaml. The resource dictionary contained an DataTemplate that was used to style several listboxes in the window. The xaml for this datatemplate contained pointers to event handlers, eg:
<Button n:Name="btnClickMe" Content="Click Me!" LeftMouseButtonUp="btnClickMe_Click" />
I recently decided to split the content of the window up into separate user controls, and to move my ResourceDictionary into it's own file. But, of course, there isn't a code-behind file for a resource dictionary file. How can I wire this up, with things split up as I've described?
Thanks in advance!

You can add a code-behind to a ResourceDictionary; just make sure your class names are referenced correctly. For instance, in the ResourceDictionary if you were working with AppStyles.xaml the XAML file would have a class of:
x:Class="Client.App.Shell.themes.AppStyles"
In the code-behind, AppStyles.xaml.cs, you would make sure to have the class:
namespace Client.App.Shell.themes
{
public partial class AppStyles
...

You can add a new class and name it with the same name as your resource dictionary plus the .cs extension and Visual Studio will automatically set things up so it becomes the code behind file.
For example if you have a resource dictionary called Buttons.xaml, add a file called Buttons.xaml.cs.

You should consider using RoutedCommands, I am thinking.
there are many many resources online, here are a couple that might help you.
http://msdn.microsoft.com/en-us/library/ms752308.aspx
http://www.devx.com/DevX/Article/37893/0/page/1

Related

How can I bind Text property of TextBox (VIEW) to a vaiable (in VIEWMODEL)

I am a newbie in WPF. I was exploring MVVM Pattern for WPF applications. I am having trouble in binding Text property of a TextBox from VIEW to a variable in VIEWMODEL
Here is the TextBox from MainWindow.xaml
<TextBox x:Name="UsernameTxt" Grid.Row="4" materialDesign:HintAssist.Hint="Username"/>
I just need to know how to bind its Text Property to ViewModel Class in Class Library
Thanks
I think it's possible to give a very generic answer to this very generic question.
If the question changes context this answer is very likely to be deleted but here goes anyhow.
You want your viewmodel to be in the datacontext of the textbox. Because datacontext is inherited down the visual tree this usually means you want to set datacontext of your window to an instance of the viewmodel. Or maybe the usercontrol your textbox is in, but we know nothing about your app so let's just cover the simple scenario.
Your options are to instantiate a viewmodel using code or xaml.
If you look at this article:
https://social.technet.microsoft.com/wiki/contents/articles/31915.wpf-mvvm-step-by-step-1.aspx
That instantiates in xaml.
Note the xmlns is
xmlns:local="clr-namespace:wpf_MVVM_Step01"
That's saying where you see some bit of markup which is prefaced "local:" then go get the class out of this namespace.
To point to a different dll ( a class library ) you need to tell it which assembly. You do that by adding ;assembly=Whicheverdll to your equivalent of that xmlns. And of course that won't be local then so give it a different name. You also need a reference to that dll or project added to the entry point exe.
Once you've done all that and your viewmodel is instantiated into memory and in the datacontext of that textbox you need some sort of binding.
Which the article covers but that will be something like:
<TextBox Text="{Binding YourPublicStringProperty}"/>

Reusing User Controls

I'm trying to alter an existing WPF application and my lack of WPF experience (I'm WINFORMS) is making this difficult.
I've come across a situation where I need to reuse a UserControl and I'm not sure how to do this in terms of modifying the xaml. I'll explain.
UserControlA has the following code:
<Grid Name="gdMain" Style="{StaticResource ContentRoot}">
<content:MonitorAlarmsPage />
</Grid>
Now, "MonitorAlarmsPage" is an AXML document that defines another UserControl - UserControlB. This UserControl, once created has to persist for the lifetime of the application.
So, I could have many UserControlAs, but only ONE UserControlB.
I've created a static class that has an appropriate UserControlB field which is updated when UserControlB is created, but how do I modify the content:MonitorAlarmsPage so that the content of the grid is replaced by the existing UserControlB as referenced in this static class and not by the XAML file that defines UserControlB? Can this actually be done? i.e. essentially, insert pre created user controls inside an XAML page.
To make things a bit clearer, UserControlB is essentially a page that can sit inside another page. The page is complex and there is a massive overhead incurred when it is created and so must only be created once.
WPF is very different to WinForms, so you certainly have your work cut out for you. The basic idea for the solution to your problem is this. You'll need to add a DependencyProperty to the MonitorAlarmsPage control. This will enable you to data bind to this property from outside the control.. This property should be of a type of custom class that you define, that contains all of the properties required in the inner control.
The next stage is to develop a DataTemplate that defines how WPF should display your custom class when it comes across an instance of it. In this DataTemplate, you declare your inner control, so that when WPF sees the custom class, it displays the inner control and sets the custom class as the DataContext for the inner control:
<DataTemplate DataType="{x:Type YourXmlNamespacePrefix:YourCustomClass}">
<YourControlsXmlNamespacePrefix:YourInnerControl DataContext="{Binding}" />
</DataTemplate>
Finally, you'll need to data bind your custom class (the static class) to the outer UserControl:
<content:MonitorAlarmsPage YourCustomClassProperty="{Binding YourStaticClass}" />
So just to be clear... this static class should be a data class, not a UI class. When using WPF, we manipulate data, not UI elements. We let the wonderful templating system generate the UI for us.

Generics in XAML views: What about the .xaml file (content of the generic class)?

I have been trying to implement a generic view in XAML, this kind of thing in the MyView.xaml.cs file:
public partial class MyView<T>: UserControl {
// content...
}
And I know that I have to use x:TypeArguments for then using MyView. That's right, there's documentation online about that.
But... What about putting something in the MyView*.xaml* file ?
Currently I have a standard:
<UserControl x:Class="Some.Name.Space.Views.MyView"
*bunch of namespaces declarations*
>
some content (a StackPanel, a grid, whatever...)
</UserControl>
But, of course, the content of the .xaml file is going to make a class MyView and not MyView<T>. And I just cannot find on the internet which specific keyword we have to use in the xaml to say we are declaring the content of a Generic view.
Thanks

How to refer to Canvas in another XAML File?

I have a problem with figuring out how to refer to a Canvas in another XAML file in my project.
For example there is a Canvas object containing some labels, images called MyLayout and is stored in MyLayout.xaml
I want to use this Canvas in the main Window of the application in Window.xaml. How can this be done? There will be multiple layouts that will be needed to be loaded into the Window.
All the XAML is within the project and compiled it cannot be loaded from a file.
How do I reference the Canvas Object in the MyLayout.xaml file in the Window.xaml?
This can be in XAML, VB.NET code or even C#.
I have searched for hours trying to figure this out. How to use an object from one XAML file in another? How is this done?
Thanks in advance. I hope someone knows how to do this.
Have you thought about using a UserControl or ControlTemplate?
Since your Canvas object is really a subclass of Canvas, that subclass is defined in your assembly. You can reference objects defined in other namespaces by adding another xmlns attribute to the root XAML object.
Something like this (assuming that your Canvas subclass is called MyCanvas in the MyNamespace namespace):
<Window x:Class="MyNamespace.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace">
<local:MyCanvas />
</Window>
You can set the properties of the MyCanvas class like any other object.

Is it possible to set code behind a resource dictionary in WPF for event handling?

Is it possible to set code behind a resource dictionary in WPF. For example in a usercontrol for a button you declare it in XAML. The event handling code for the button click is done in the code file behind the control. If I was to create a data template with a button how can I write the event handler code for it's button click within the resource dictionary.
I think what you're asking is you want a code-behind file for a ResourceDictionary. You can totally do this! In fact, you do it the same way as for a Window:
Say you have a ResourceDictionary called MyResourceDictionary. In your MyResourceDictionary.xaml file, put the x:Class attribute in the root element, like so:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MyCompany.MyProject.MyResourceDictionary"
x:ClassModifier="public">
Then, create a code behind file called MyResourceDictionary.xaml.cs with the following declaration:
namespace MyCompany.MyProject
{
partial class MyResourceDictionary : ResourceDictionary
{
public MyResourceDictionary()
{
InitializeComponent();
}
... // event handlers ahead..
}
}
And you're done. You can put whatever you wish in the code behind: methods, properties and event handlers.
== Update for Windows 10 apps ==
And just in case you are playing with UWP there is one more thing to be aware of:
<Application x:Class="SampleProject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:rd="using:MyCompany.MyProject">
<!-- no need in x:ClassModifier="public" in the header above -->
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- This will NOT work -->
<!-- <ResourceDictionary Source="/MyResourceDictionary.xaml" />-->
<!-- Create instance of your custom dictionary instead of the above source reference -->
<rd:MyResourceDictionary />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
I disagree with "ageektrapped"... using the method of a partial class is not a good practice. What would be the purpose of separating the Dictionary from the page then?
From a code-behind, you can access a x:Name element by using:
Button myButton = this.GetTemplateChild("ButtonName") as Button;
if(myButton != null){
...
}
You can do this in the OnApplyTemplate method if you want to hookup to controls when your custom control loads. OnApplyTemplate needs to be overridden to do this. This is a common practice and allows your style to stay disconnected from the control. (The style should not depend on the control, but the control should depend on having a style).
Gishu - whilst this might seem to be a "generally not to be encouraged practice" Here is one reason you might want to do it:
The standard behaviour for text boxes when they get focus is for the caret to be placed at the same position that it was when the control lost focus. If you would prefer throughout your application that when the user tabs to any textbox that the whole content of the textbox was highlighted then adding a simple handler in the resource dictionary would do the trick.
Any other reason where you want the default user interaction behaviour to be different from the out of the box behaviour seems like good candidates for a code behind in a resource dictionary.
Totally agree that anything which is application functionality specific ought not be in a code behind of a resource dictionary.
Adding on....these days, with the advent of {x:Bind ...}, if you want to put your DataTemplate into a shared ResourceDictionary file, you are required to give that file a code behind.
XAML is for constructing object graphs not containing code.
A Data template is used to indicate how a custom user-object is to be rendered on screen... (e.g. if it is a listbox item) behavior is not part of a data template's area of expertise. Redraw the solution...

Resources