I have a UserControl which references a StaticResource which would normally be referenced in app.xaml and be fine... but my assembly is a library project so has no app.xaml. How do i reference this StaticResource now?
Here is the UserControl where I am trying to refence it
<UserControl
d:DataContext="{Binding Source={x:Type main:IViewModel},
Converter={StaticResource viewModelLocator}}">
and here is where it would normally be in app.xaml
<Application xmlns:t="http://schemas.t.com/wpf" xmlns:app="clr-namespace:T.UI">
<Application.Resources>
<t:ViewModelLocator
x:Key="viewModelLocator"
Container="{x:Static app:ConfigurationPlugin.Container}" />
</Application.Resources>
I just get the error message "viewModelLocator could not be resolved".
adding the resource to the UserControl's resources itself should do the trick; in fact pretty much every Wpf element has a Resources property.
<UserControl.Resources>
<t:ViewModelLocator x:Key="viewModelLocator"
Container="{x:Static app:ConfigurationPlugin.Container}" />
</UserControl.Resources>
<UserControl.Resources>
....
unless I don't understand your question.
Related
I'm looking to set a style on some UserControls that's kept in a resource dictionary at the base of my project tree. The only way I've found to do so is below by defining MergeDictionary in the resources to link in the xaml file with styles. I'd prefer to not have to put this same code on every single control that needs this style though. Is there a better way to do this?
<UserControl
x:Class="TestApp.Screens.Sub.Details"
...
Style="{DynamicResource BottomContentUserControl}">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../../GeneralStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
</UserControl>
You can add the styles to the ApplicationResourceDictionary or you could add them to your App.xaml file so that they will be applied throughout your application.
I have user HeaderedItemsControl to display different UserControl in my application.
Now I declared these UserControl with DataTemplate as follows in my MainWindow.
<HeaderedItemsControl.Resources>
<DataTemplate DataType="{x:Type vm:SearchViewModel}">
<vw:SearchStudentView/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:SearchViewModel2}">
<vw:SearchStudentView2/>
</DataTemplate>
</HeaderedItemsControl.Resources>
But I have almost 20 view and I want to place all DataTemplate in ResourceDictionary.
Can any one help me how can I use these DataTemplate's from ResourceDictionary in HeaderedItemsControl's resources?
It's very simple.
1) You must create and add DataTemplate to ResourceDictionary.
2) In App.xaml file you must add created ResoueceDictionary (TemplateResourceDictionary.xaml is my test ResourceDictionary).
<Application x:Class="ResourceDictionaryExample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="TemplateResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
That's all. :)
lets assume we have the following control definition
<ctrl:ChildWindow x:Class="Control.Editor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctrl="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:local="clr-namespace:Control"
Width="400" Height="300"
local:AttachedProperties.DialogResult="{Binding Path=DialogResult}"
Title="{Binding Path=Caption}" Style="{StaticResource Title}" DataContext="{Binding}" HasCloseButton="False">
<ctrl:ChildWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Control;component/Resources/BaseAppearance.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</ctrl:ChildWindow.Resources>
</ctrl:ChildWindow>
The problem is that the style on the root control cannot be set because the ResourceDictionary is not loaded.
How can I get access to the StaticResource Title during the initialization of the control, when I don't have access to the App class? I'm also not sure that it would be possible, if I would have access to it.
Regards
I'd recommend accessing your resource and doing the work in the .Loaded() event of your control.
Edit: On second thought... I think I know what you're doing now... You have a resource set in your App.xaml class, but you want to access it in your control.
Easy way around the problem is to set it to a DynamicResource instead... but this is less performant.
What is the BuildAction set to on your App.xaml in the property's tab?
If it is ApplicationDefinition... then you should be able to access your resource as you currently are.
I found the common way without using code behind. I knew it is possible. ^^
<ctrl:ChildWindow x:Class="Control.Editor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctrl="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:local="clr-namespace:Control"
Width="400" Height="300"
local:AttachedProperties.DialogResult="{Binding Path=DialogResult}"
Title="{Binding Path=Caption}" DataContext="{Binding}" HasCloseButton="False">
<ctrl:ChildWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Control;component/Resources/BaseAppearance.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</ctrl:ChildWindow.Resources>
<crtl:ChildWindow.Style>
<StaticResource ResourceKey="Title" />
</crtl:ChildWindow.Style>
</ctrl:ChildWindow>
I am trying to add resource dictionary into my silverlight-4 aplication (suggested in "Applying a View to a ViewModel" chapter of the http://msdn.microsoft.com/en-us/magazine/dd419663.aspx article).
The 1st problem: I don't see any resource in my MainPage. Am I correctly understand that I need to add resource dictionary manually into Silverlight app?
The 2nd: When I did that, in the Dictionary1.xaml file
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib">
<DataTemplate DataType="{x:Type vm:MyViewModel}">
<vw:MyView />
</DataTemplate>
</ResourceDictionary>
I am getting an error: Can't resolve a symbol 'DataType'...
Is there any idea hot to do that?
ad 1: MainPage has a ResourceDictionary. You add elements to it in xaml like this:
<MainPage>
<MainPage.ResourceDictionary>
<DataTemplate>
<vw:MyView />
</DataTemplate>
</MainPage.ResourceDictionary>
...
You can add a ResourceDictionary to the MainPage.ResourceDictionary by using the Source and MergedDictionaries properties of ResourceDictionary:
<MainPage>
<MainPage.ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</MainPage.ResourceDictionary>
...
ad 2: DataTemplate does not have a Property DataType in the Silverlight framework. :-(
You also need to add an x:Key into the DataTemplate if it's going to be in the ResourceDictionary.
In my MainWindow.xaml, I have the following reference to a ResourceDictionary:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
In MainSkin.xaml, I define a datatemplate:
<DataTemplate x:Key="TagTemplate">
...
</DataTemplate>
Deeper within my application, I attempt to use this data template:
<ContentControl DataContext="{Binding Tag}" ContentTemplate="{StaticResource TagTemplate}"/>
The code compiles successfully, but when I attempt to load a Page or UserControl that contains this StaticResource, I get an exception saying that the TagTemplate can't be found.
What am I doing wrong?
In order to access the contents of a resource defined in a XAML file, you need to "include" that XAML file in each page and control that uses it. So every XAML files will need to have the MergedDictionaries entry that you have in MainWindow.xaml.
Alternatively you can add those merge dictionaries to App.xaml and those resources are included implicitly:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Are you using that StaticResource in the same Window where it is declared? Otherwise I think that you cannot have access to that.