Situation :
I'm currently trying to connect my DataContext to my ViewModel. I'm using GalaSoftMvvmLight.
But the fact is that I don't have a Window because I'll integrate this code in another program, which has a Window. So I just have UserControl.
Problem :
I don't know why, but I can't connect my DataContext in UserControl.
I get this error {"Cannot find resource named 'Locator'. Resource names are case sensitive."}
Question :
How can I connect properly my App.xaml resources to my View ? And if it's not possible without Window, how can I call DataContext with something like this
<UserControl.DataContext>
SOMETHING TO SET DATACONTEXT WITH BINDING !
</UserControl.DataContext>
Here is my code :
App.xaml
<Application x:Class="SOMETHING.App" xmlns:vm="clr-namespace:SOMETHING.ViewModel" StartupUri="ApplicationView.xaml">
<Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator x:Key="Locator" />
</ResourceDictionary>
</Application.Resources>
</Application>
ApplicationView.xaml
<UserControl x:Class="SOMETHING.View.ApplicationView"
<!-- THIS ONE DOESN'T WORK -->
DataContext="{Binding ApplicationVM, Source={StaticResource Locator}}">
<!-- THIS ONE DOESN'T WORK IF I SET <vm:ViewModelLocator x:Key="Locator" /> IN STYLE.XAML, BUT I CAN'T USE IT IN USERCONTROL PARAMETERS (LIKE ABOVE) -->
<UserControl.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries >
<ResourceDictionary Source="Style.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Label Content="{Binding Title}" />
</Grid>
</UserControl>
ViewModelLocator
public class ViewModelLocator
{
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
if (ViewModelBase.IsInDesignModeStatic)
{
SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
}
else
{
SimpleIoc.Default.Register<IDataService, DataService>();
}
SimpleIoc.Default.Register<ApplicationViewModel>();
}
public ApplicationViewModel ApplicationVM
{
get { return SimpleIoc.Default.GetInstance<ApplicationViewModel>();
}
}
Provided that the ViewModelLocator class and the UserControl reside in the same project/assembly, you could define the ViewModelLocator resource in a ResourceDictionary that you merge into the UserControl like this:
<UserControl ...>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Global.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<UserControl.DataContext>
<Binding Path="ApplicationVM" Source="{StaticResource Locator}" />
</UserControl.DataContext>
<Grid>
</Grid>
</UserControl>
Global.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfControlLibrary1">
<local:ViewModelLocator x:Key="Locator" />
</ResourceDictionary>
Related
I have a WPF application where I have two stylesheets xaml. But I need to apply base style i.e PresentationFramework.Royale to only one of them, which is not directly loaded in App.xaml.
Below is my App.xaml. I do not want to refer PresentationFramework.Royale here,
<Application
x:Class="UI.Desktop.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/UI.Common;component/Style.Shui.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
I have a login view usercontrol to which i need to add PresentationFramework.Royale as base style along with another stylesheet. How can I achieve this,
<UserControl
x:Class="Views.Implementation.Common.LoginView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Royale, Version=3.0.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/royale.normalcolor.xaml" />
<ResourceDictionary Source="pack://application:,,,/Test.APP.UI.Common;component/Style.Test1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Button
x:Name="Login"
Grid.Row="4"
Grid.Column="2"
Width="85"
Margin="0,0,15,0"
HorizontalAlignment="Left"
Content="Login"
IsDefault="True"
TabIndex="8"
ToolTip="Login" />
</UserControl>
The issue is above code Style.Test1.xaml controls does not take royale.normalcolor.xaml as base. But it works if I put royale.normalcolor.xaml reference in App.xaml
I have this ResourceDictionary:
<DataTemplate DataType="{x:Type vm:MainViewModel}">
<ListView>
<ListView.ItemTemplate>
<DataTemplate /> <----- Here I'd like to load an external DataTemplate from the same folder
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
In the same folder I have another ResourceDictionary:
<DataTemplate DataType="{x:Type vm:EditRecordViewModel}">
<StackPanel>
<TextBlock Text="Test" />
</StackPanel>
</DataTemplate>
Question
In my first ResourceDictionary how do I get the 2nd ResourceDictionary to display in the first, where I have the <DataTemplate /> displayed?
I have added resources in the App.xaml like so, but how do actually use them?:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Views/MainViewModel.xaml" />
<ResourceDictionary Source="Views/EditRecordViewModel.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
You can provide a key to a data template
<DataTemplate x:Key="test" DataType="{x:Type local:EditRecordViewModel}">...
and then reference it
<ListView ItemTemplate="{StaticResource test}">...
You have declared Data Templates without a key, which means that they will be applied to the corresponding types by default.
You don't need any links for this.
The only thing that matters is that the ListView gets a collection of items of type EditRecordViewModel.
Example.
TwoDataTemplte/ViewModels.cs:
namespace TwoDataTemplte.ViewModel
{
public class EditRecordViewModel
{
public string Text { set; get; }
}
public class MainViewModel
{
public EditRecordViewModel[] EditRecords { get; } =
{
new EditRecordViewModel() {Text = "First"},
new EditRecordViewModel() {Text = "Second"}
};
}
}
TwoDataTemplte\MainDictionary.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:TwoDataTemplte.ViewModel">
<DataTemplate DataType="{x:Type vm:MainViewModel}">
<ListView ItemsSource="{Binding EditRecords}"/>
</DataTemplate>
</ResourceDictionary>
TwoDataTemplte\ItemDictionary.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:TwoDataTemplte.ViewModel">
<DataTemplate DataType="{x:Type vm:EditRecordViewModel}">
<StackPanel>
<TextBlock Text="{Binding Text}" />
</StackPanel>
</DataTemplate>
</ResourceDictionary>
App.xaml:
<Application x:Class="Febr20y.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Febr20y"
StartupUri="TwoDataTemplte/ExampleWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="TwoDataTemplte/MainDictionary.xaml" />
<ResourceDictionary Source="TwoDataTemplte/ItemDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
TwoDataTemplte\ExampleWindow.xaml:
<Window x:Class="TwoDataTemplte.ExampleWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TwoDataTemplte"
xmlns:vm="clr-namespace:TwoDataTemplte.ViewModel"
mc:Ignorable="d"
Title="ExampleWindow" Height="450" Width="800">
<Grid>
<ContentControl>
<vm:MainViewModel/>
</ContentControl>
</Grid>
</Window>
Using .NET Framework 4.6.1 and I'm using a UI kit that I've installed via NuGet and they are referenced correctly in the project.
App.xaml
<Application x:Class="ExampleApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:adonisUi="clr-namespace:AdonisUI;assembly=AdonisUI"
xmlns:local="clr-namespace:ExampleApp"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="{x:Static adonisUi:ResourceLocator.DarkColorScheme}" />
<ResourceDictionary Source="{x:Static adonisUi:ResourceLocator.ClassicTheme}" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
MainWindow.xaml
<Window x:Class="ExampleApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ExampleApp"
mc:Ignorable="d"
Title="MainWindow"
Height="450"
Width="800">
<Window.Style>
<Style TargetType="Window"
BasedOn="{StaticResource {x:Type Window}}" />
</Window.Style>
<Grid>
<StackPanel Margin="10">
<Button Content="Click Me"
HorizontalAlignment="Center" />
</StackPanel>
</Grid>
</Window>
Issues:
<ResourceDictionary Source="{x:Static adonisUi:ResourceLocator.DarkColorScheme}" />
<ResourceDictionary Source="{x:Static adonisUi:ResourceLocator.ClassicTheme}" />
Both lines in App.xaml are getting the following error:
Value cannot be null. Parameter name: item.
I have tried multiple fresh projects, building and rebuilding and I keep getting this error. I am able to build the project and I can see the styles from the UI kit correctly applied on MainWindow even though the error is still there.
However the styles don't appear on the designer window, I'm not sure if it's related to the error I'm getting or not.
Any ideas what could cause this?
I just tried it and it does the same thing to me... using Visual Studio Enterprise 2017 15.9.7
If you look at AdonisUI.ResourceLocator in a decompiler (I used Telerik's JustDecompile), you'll see the definitions:
public static Uri ClassicTheme
{
get
{
return new Uri("pack://application:,,,/AdonisUI.ClassicTheme;component/Resources.xaml", UriKind.Absolute);
}
}
public static Uri DarkColorScheme
{
get
{
return new Uri("pack://application:,,,/AdonisUI;component/ColorSchemes/Dark.xaml", UriKind.Absolute);
}
}
public static Uri LightColorScheme
{
get
{
return new Uri("pack://application:,,,/AdonisUI;component/ColorSchemes/Light.xaml", UriKind.Absolute);
}
}
If you change your App.xaml to reference using these values then it works.
<ResourceDictionary Source="pack://application:,,,/AdonisUI.ClassicTheme;component/Resources.xaml" />
<ResourceDictionary Source="pack://application:,,,/AdonisUI;component/ColorSchemes/Dark.xaml" />
I have just started working with Resource Dictionaries and I am stuck on this because my resource dictionary is not working at all. I have tried code-behind and XAML but every time I get exceptions and the app crashes.
If I reference the Dictionary through XAML I get the exception at runtime that Name/Key is not found. The code I used in App.xaml is:
<Application
x:Class="WatchfreeWebsite.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WatchfreeWebsite.Helpers">
<Application.Resources>
<TransitionCollection x:Key="TransCollection">
<EdgeUIThemeTransition Edge="Right"/>
</TransitionCollection>
<ResourceDictionary x:Key="resourcesDictionary">
<ResourceDictionary.MergedDictionaries>
<local:GlobalTemplates Source="Helpers/GlobalTemplates.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
The resource dictionary holds aDataTemplate and a MediaTransportControlsStyle but I cant seem to reference it through XAML because it gives syntax errors and during the runtime the page produces exception while loading XAML at InitializeComponent(); stage.
Resource Dictionary:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WatchfreeWebsite.Helpers"
x:Class="WatchfreeWebsite.Helpers.GlobalTemplatesClass"
xmlns:data="using:WatchfreeWebsite.Helpers">
<DataTemplate x:Key="StreamBoxItemTemplate"
x:DataType="data:StreamingHelper">
<TextBlock Text="{x:Bind StreamName, Mode=OneWay}"
Style="{StaticResource BodyTextBlockStyle}"
TextWrapping="NoWrap"
MaxLines="1"
TextTrimming="WordEllipsis"/>
</DataTemplate>
<Style TargetType="MediaTransportControls"
x:Key="myCustomTransportControls">
<Setter Property="IsTabStop" Value="False" />
.......
</Style>
</ResourceDictionary>
The class behind the resource dictionary is:
public partial class GlobalTemplatesClass
{
public GlobalTemplatesClass()
{
this.InitializeComponent();
}
}
I reference the DataTemplate inside the above style and this style is referenced in another page as:
<MediaPlayerElement x:Name="MediaView"
Grid.Row="2"
Source="{Binding MediaSourceObject, Mode=OneWay}"
DoubleTapped="MediaView_DoubleTapped"
AreTransportControlsEnabled="True">
<MediaPlayerElement.TransportControls>
<data:CustomTransportControlsHelper Style="{StaticResource ResourceKey=myCustomTransportControls}"/>
</MediaPlayerElement.TransportControls>
</MediaPlayerElement>
But this is not working and there is a red line below the resource name saying that the resource is not found.
Is there something that I am missing? If someone can help me here please provide your suggestions. Thanks
When you add multiple items under your resources, each of them should fall within the <ResourceDictionary> tag and not directly under <Application.Resources>.
That's because Resources itself is a dictionary, so you're in effect trying to replace that collection rather than add elements to it. Docs here: https://msdn.microsoft.com/en-us/library/system.windows.application.resources%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
I created a sample project with just an App.xaml at the project base level, a folder called Helpers and a ResourceDictionary under Helpers named GlobalTemplates.xaml to match yours.
Created a simple brush as an example in GlobalTemplates.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1.Helpers">
<SolidColorBrush x:Key="DefaultForeground" Color="DarkGreen" />
</ResourceDictionary>
In App.xaml
<Application
x:Class="App1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
RequestedTheme="Light">
<Application.Resources>
<ResourceDictionary>
<TransitionCollection x:Key="TransCollection">
<EdgeUIThemeTransition Edge="Right"/>
</TransitionCollection>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Helpers/GlobalTemplates.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
And then in MainPage.xaml successfully referenced the style from the dictionary:
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Foreground="{StaticResource DefaultForeground}">Hello world</TextBlock>
</Grid>
</Page>
I created a CParametres object in my app.xaml.cs like this :
public partial class App : Application
{
public CParametres myParamObject;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
myParamObject = new CParametres(System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) +#"\BingMapsParam.ini");
if (myParamObject.LoadParams() == false)
{
return;
}
Resources.Add("myParamObject", myParamObject);
}
}
Now, in my app.xaml, i add a Dictionnary :
<Application x:Class="myGeoloc.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="MyDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
And, here is my Dictionnary :
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:t="clr-namespace:myGeoloc"
xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF">
<t:CParametres x:Key="myParamObject"/>
<Style TargetType="m:Pushpin" x:Key="PushpinStyle_Fournisseur">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="m:Pushpin" >
<!-- <Image Stretch="Fill" Source="C:\Users\FabioWalter\Documents\Visual Studio 2013\Projects\myGeoloc\myGeoloc\bin\Debug\Pushpins\PushPinStandard.png" />-->
<Image Stretch="Fill" Source="{Binding Path=strPicturePushpinFournisseur, Source={StaticResource myParamObject}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Width" Value="64" />
<Setter Property="Height" Value="64" />
</Style>
</ResourceDictionary>
strPicturePushpinFournisseur is string in CParametres. This string contain the picture paths.
Actually, the image don't display and it's related to my bad binding.
Anyone could help me please ?
Any ideas ?
Thanks a lot :)
You can do some thing like this:
To put the object in the resource dictionary for making it available to the rest of the application
<Application x:Class="SomeNameSpace.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:t="clr-namespace:SomeNameSpace.NameSpaceForT"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="View/DictionaryResources/MainResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
<t:CParametres x:Key="myParamObject"/>
</ResourceDictionary>
</Application.Resources>
</Application>
Make a binding to any object's property (This is a sample):
<Window x:Class="SomeNameSpace.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:View="clr-namespace:SomeNameSpace.View"
Title="MainWindow"
DataContext="{Binding Path=strPicturePushpinFournisseur, Source={StaticResource myParamObject}}"
Height="350" Width="525">
...
EDIT
You can create the object in the App.xaml.cs file, and add contructor parameters in this way:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Resources.Add("myParamObject", new CParametres ("Param1", "Param2"));
}
}
And use it in this way:
<TextBlock Text="{Binding Path=strPicturePushpinFournisseur, Source={StaticResource myParamObject}}"></TextBlock>
Hope it helps...