I would like to place some WPF Data Templates in a loose file that I can reference where needed in my UserControl. I do not want the Data Templates to be application wide (use app.config), I only want it to be specific to my library.
Is there a way to do this besides placing the Data Templates in UserControls and then loading the UserControls?
Thanks.
Create a ResourceDictionary and put them in that. You can then load the ResourceDictionary and access the contained DataTemplates using indexer syntax:
DataTemplate myTemplate = (DataTemplate)rd["MyTemplate"];
The ResourceDictionary is a XAML file which you would compile into your library just as you would with a UserControl or Window. If you want to ship the templates as loose (uncompiled) XAML then you can still use a ResourceDictionary, but would need to use XamlReader to load it from the .xaml source file.
Related
I've looked at using a control template to use the same XAML code between two views. However, where do I store this Control template so both views can access it and therefore display the controls I need?
Thanks
If both views are under same window, you can store ControlTemplate under Window.Resources and if both view belongs to different windows store that under Application.Resources.
I am trying to use a ResourceDictionary in loose XAML and load it a runtime to provide templates and styles to a WPF app. I have the XAML available in a local directory and am adding a new ResourceDictionary to App.Current.Resources.MergedDictionaries in app startup using a URI.
When the XAML goes to parse, it blows up on a template where the TargetType is a custom control from the assembly that is consuming it.
The specific message is:
'Failed to create a 'Type' from the text 'controls:CustomType'.'
I already have the namespace mapped in the ResourceDictionary at the top:
xmlns:controls="clr-namespace:TEST.UI.WPF.Common.Controls"
There are articles out there stating that loading ResourceDictionaries from loose XAML is possible but none of them that I have found address custom types within those loose XAML files.
Any help is appreciated!
Since you are loading this at runtime and it is not built within the project then the ResourceDictionary does not know of your assembly. You will have to include the assembly in the namespace for it to be recognised.
xmlns:controls="clr-namespace:TEST.UI.WPF.Common.Controls;assembly=Common"
You should be able to run you app after you add ';assembly=yourAssembly' to the namespace declaration.
I would like to name all unnamed controls in the xaml files in my project to support automated UI testing.
Can anyone think of way to report on all controls, in all .xaml files in a project without the x:Name attribute defined?
Naming the controls will be manual, but I would like to be able to have a test that fails if any .xaml document contains an unnamed control.
I'm trying to work out the best way to couple my Views and ViewModels in MVVM and I've settled on the ViewModel-first approach using typed DataTemplates, as described in this article and in this answer. I'm using Prism and have multiple modules which have their own projects/class libraries.
My question is: Where should my DataTemplates live in my solution?
Should I put the DataTemplates in a Resource Dictionary which lives in the same project that has the types/ViewModels it renders?
Should I put the DataTemplates in a Resource Dictionary which lives in the project which has the application's MainWindow (i.e. Shell.xaml)?
Should these Resource Dictionaries then be added to App.Current.MainWindow.Resources.MergedDictionaries?
I hope this is enough information to describe what I'm trying to do.
Update: see comments of selected answer.
I'm sure that the best way here is to use Themes\Generic.xaml resources file. This is file (it should be exactly in folder Themes and has name exactly Generic.xaml) used by WPF/Silverlight themes engine and contains resources shared through whole application. You can also create separate file in folder Themes with name like Generic.DataTemplates.xaml and add link to it from Generic.xaml. Google knows a lot about generic.xaml or you can see more details in my answer here: WPF Prism - Where to put Resources?
Updated to explain more clear.
I will say if your DataTemplate is generic:
i.e You have a UserControl that binds to a ViewModel, and that ViewModel has BaseViewModel, which expose some sort of properties. Your DataTemplate is displaying those properties. So you can use this DataTemplate on every ViewModel that Implement the BaseViewModel.
Is better to put it in App.xaml, so you will able to pull it out with the Key and apply on different place in your project.
But if your DataTemplate is very specific,
i.e There is a UserControl that only binds on the specified property in that ViewModel and you know no other control will binds to that ViewModel, you will want to put into the same Xaml file's Resources or where you define your UserControl.
According to Microsofts App Studio the DataTemplates should live in a DataTemplates Subdirectory under the Views Directory. A Universal app has this directory for both the Windows UI as for the Windows Phone UI so its not in the Shared project because they are not the Same. Don't use the Converge PRISM architecture. Its completely wrong designed! That was not written with a Windows and a Windows Phone architecture in mind but like they call it Converged. It should have been completely redesigned like it works in Microsofts App Studio. Don't look for Dependency Injection its not in it and not needed. Most use Dependency Injection for stub or fake interfaces. The DataContext for design data works now so good with json data that a Dependency Injection component would be overkill.
We have a WPF application that is loading a usercontrol at runtime from the deployed .XAML file (customer requirement - they want to be able to replace the views entirely).
We would like to localize strings in this XAML file. All .resx file based WPF localization approaches I have found on the web seem to require recompilation when the localized values are changed.
How to localize a dynamically loaded XAML usercontrol without needing to recompile the resources?
This solution seems to provide a markup extensiion that manages localization loaded from XML files.
Syntax:
<TextBlock loc:Translate.Uid="3"
Text="{loc:Translate}"
Background="{loc:Translate}"
Width="{loc:Translate}"
Height="{loc:Translate}" FontSize="18"
/>
It also supports on the fly language selection (no need to reopen the window).