Having got IntelliSense for Data Binding working in a simple test application, thanks to the answer to my previously raised question, I'm now trying to apply what I've learnt to the actual application I'm working on. Again I'm encountering problems that I don't understand. A snippet of my code is below - I've had to change names to protect propriety information:
<Page x:Class="MyProject.Views.Pages.MyPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="450"
xmlns:Converters="clr-namespace:MyProject.Converters"
xmlns:ViewModels="clr-namespace:MyProject.ViewModels"
Title="My View"
SnapsToDevicePixels="True" KeepAlive="True" TextOptions.TextFormattingMode="Display">
<Page.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
<Converters:NullToVisibilityConverter x:Key="NullToVisibilityConverter"/>
</Page.Resources>
<StackPanel d:DataContext="{d:DesignInstance ViewModels:MyViewModel}">
<!-- ... -->
</StackPanel>
</Page>
I'm getting an error message on the line <StackPanel d:DataContext="{d:DesignInstance ViewModels:MyViewModel}">:
The name "MyViewModel" does not exist in the namespace "clr-namespace:MyProject.ViewModels".
The error doesn't make sense MyViewModel does exist within the MyProject.ViewModels namspace.
Any suggestions? I've tried a clean rebuild.
The MyProject.ViewModels namepsace is within a different assembly to the MyProject.Views.Pages and it appears to necessary to add ;assembly=MyProject.ViewModels to the xmlns:ViewModels="clr-namespace:MyProject.ViewModels delcaration:
xmlns:ViewModels="clr-namespace:MyProject.ViewModels;assembly=MyProject.ViewModels"
I assumed that because the assembly is referenced by the project, I wouldn't need to specify an assembly, just as I don't have to specify an assembly when using a namespace within a C# code file.
I just got the same error and I got it many times before. Each time I have this error, it come from the fact that I have other errors and my code won't compile for other reasons. As soon as I fixed all other errors, my code compile and VisualStudio find all 'd:' missing references... if they really exists.
Also, as an alternative, if you instanciate your ViewModel with its default constructor, I suggest to use something like (without using 'd:'):
...
</Page>
<Page.DataContext>
<ViewModels:MyViewModel/>
</Page.DataContext>
It will solve your problem forever.
I haven't tried it yet, but perhaps that with Roslyn (the new VS2015 compiler) this problem will go away. I hope :-)
Related
I am using MahApps.Metro controls in my XAML code for the toolbox in Visual Studio Extension. I installed the package via NuGet, then I tried to add a control into my XAML markup. Below is the code snippet.
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:custom="http://metro.mahapps.com/winfx/xaml/controls"
x:Class="AutoDebug.MyControl"
Background="{DynamicResource VsBrush.Window}"
Foreground="{DynamicResource VsBrush.WindowText}"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="400"
DataContext="{Binding UserControlModel}"
x:Name="AutoDebugWindow">
<Grid Margin="15">
<custom:Tile Content="Tile" HorizontalAlignment="Left" Margin="75,150,0,0" VerticalAlignment="Top" Background="#FF8B00BF"/>
</Grid>
</UserControl>
But I receive the following error no matter what.
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: Could not load file or assembly 'MahApps.Metro, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
I have already tried installing/uninstalling, deleting/adding references but nothing has worked so far.
This is caused by the fact that MahApps.Metro is not included as a reference when the Visual Studio extension is compiled.
I'm not entirely sure why, but if you only use MahApps in XAML, then no reference is included in the compiled assembly. You can check this by unpackaging the extension (it's just a zip file), and opening the assembly in ILSpy. Under the references, MahApps will not be listed.
A workaround for this is to use MahApps somewhere in code. The simplest way to do this is to name the MahApps control that you are using. This generates a field for the control, and that seems to be enough to cause a reference to be included in the assembly.
<Grid Margin="15">
<custom:Tile x:Name="MyTile" />
</Grid>
You can also use an object from the MahApps assembly anywhere else in code (for example, you could create a new object in the constructor of the Package), but giving one of the controls a name is probably the simplest way.
xmlns:viewModel="clr-namespace:LoginViewModule.ViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
d:DataContext="{Binding Source={viewModel:LoginContentViewModel}}">
It is saying that:
LoginContentViewModel doesnt exists and invalid markup extension.
Can somebody please tell me what I'm doing wrong?
Try rebuilding first, it may just be that it can't see the ViewModel yet.
If that doesn't work you can do it like this, though this is not ignored by the xaml processor:
<Window.DataContext>
<viewModel:LoginContentViewModel />
</Window.DataContext>
Or if you are wishing for it to be ignored by the xaml processor this should work:
d:DataContext="{Binding viewModel:LoginContentViewModel}"
EDIT: Make sure your namespaces are correct, otherwise none of the above will have any effect.
Your folder structure does not necessarily mean that your namespaces are in that layout. You need to check the LoginContentViewModel.cs to make sure that the namespace there is LoginViewModule.ViewModel and not just LoginViewModule which would have occurred if you added your files to your root directory and then moved them into the folder
I have been trying to implement a behavior on a wpf window therefore I have added reference to System.Winodws.Interactivity in my current solution and then wrote the desired behavior. but in order to apply this behavior, I have to write something like this in Windows XAML.
<Window x:Class="WpfApplication5.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"
xmlns:behav ="clr-namespace:WpfApplication5"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity;assembly=System.Windows.Interactivity">
<Window.Resources>
<i:Interaction.Behaviors>
<behav:DialogIconRemoveBehavior></behav:DialogIconRemoveBehavior>
</i:Interaction.Behaviors>
</Window.Resources>
<Grid>
</Grid>
</Window>
but this is not valid tag because I might have to add reference to any other assembly apart form System.Windows.Interactivity, So, please suggest what else
I have to do in order to use tag in XAML
After wasting my one hour I came to know that only I had to include System.Windows.Interactivity as reference and which I had already done.
the issue was that Behaviors can not be declared in the Resource part of any control.
the moment I took the following
<i:Interaction.Behaviors>
</i:Interaction.Behaviors>
out of the resources then it worked fine.
So, conclusion only System.Windows.Interacivity is the only required.
Never declare that behavior part in the resources or style.
I am converting some Silverlight XAML into WPF. I currently have a user control (MyControl) that is trying to include a couple of other controls that are custom buttons (MyButton1) that are within the same assembly. I have the following XAML that compiles and works in SL:
MyButton1
<UserControl
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"
mc:Ignorable="d"
x:Class="MyCompany.MyNamespace.MySubnamespace.MyButton1"
d:DesignWidth="640" d:DesignHeight="480">
...
</UserControl>
MyControl
<UserControl
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:somename="clr-namespace:MyCompany.MyNamespace.MySubnamespace;assembly=MyCompany.MyAssemblyName"
mc:Ignorable="d"
x:Class="MyCompany.MyNamespace.MySubnamespace.MyControl"
d:DesignWidth="640" d:DesignHeight="480">
<somename:MyButton1 />
</UserControl>
When I try to compile this code in WPF, I get the following error:
The tag 'MyButton1' does not exist in XML namespace 'clr-namespace:MyCompany.MyNamespace.MySubnamespace;assembly=MyCompany.MyAssemblyName.'
The weird thing is, if I comment out the <somename:MyButton1 /> line of code and compile and then type in <somename: IntelliSense gives me the option to autocomplete MyButton1. Which suggests that the control itself is in the assembly but for some reason it is not being seen when the MyControl XAML is being compiled.
For some context, I took the SL csproj file and made some modifications to it manually to make it a WPF csproj file. If there is a possibility that this caused this funkiness to happen, I'd be glad to share relevant portions of the project file.
Found the answer on an MSDN forum. Turns out that the assembly=MyCompany.MyAssemblyName line in my xmlns was screwing things up. Once I removed that line, I was able to reference the controls.
Related Link: http://social.msdn.microsoft.com/Forums/en/wpf/thread/807c9b80-81c7-4f75-aa2f-8427e17b1a90
To reference the current assembly you must not type its name but leave it blank, i.e. assembly=, the other option is of course to drop it completely. (MSDN -> Mapping to Current Assemblies)
I have the following XAML below for the main window in my WPF application, I am trying to set the design time d:DataContext below, which I can successfully do for all my various UserControls, but it gives me this error when I try to do it on the window...
Error 1 The property 'DataContext' must be in the default namespace or in the element namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'. Line 8 Position 9. C:\dev\bplus\PMT\src\UI\MainWindow.xaml 8 9 UI
<Window x:Class="BenchmarkPlus.PMT.UI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:UI="clr-namespace:BenchmarkPlus.PMT.UI"
xmlns:Controls="clr-namespace:BenchmarkPlus.PMT.UI.Controls"
d:DataContext="{d:DesignInstance Type=UI:MainViewModel, IsDesignTimeCreatable=True}"
Title="MainWindow" Height="1000" Width="1600" Background="#FF7A7C82">
<Grid>
<!-- Content Here -->
</grid>
</Window>
I needed to add the mc:Ignorable="d" attribute to the Window tag. Essentially I learned something new. The d: namespace prefix that Expression Blend/Visual Studio designer acknowledges is actually ignored/"commented out" by the real compiler/xaml parser!
<Window
...
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
...
/>
The following was taken from
Nathan, Adam (2010-06-04). WPF 4 Unleashed (Kindle Locations 1799-1811). Sams. Kindle Edition.
Markup Compatibility
The markup compatibility XML namespace (http://schemas.openxmlformats.org/markup-compatibility/2006, typically used with an mc prefix) contains an Ignorable attribute that instructs XAML processors to ignore all elements/attributes in specified namespaces if they can’t be resolved to their .NET types/members. (The namespace also has a ProcessContent attribute that overrides Ignorable for specific types inside the ignored namespaces.)
Expression Blend takes advantage of this feature to do things like add design-time properties to XAML content that can be ignored at runtime.
mc:Ignorable can be given a space-delimited list of namespaces, and mc:ProcessContent can be given a space-delimited list of elements. When XamlXmlReader encounters ignorable content that can’t be resolved, it doesn’t report any nodes for it. If the ignorable content can be resolved, it will be reported normally. So consumers don’t need to do anything special to handle markup compatibility correctly.
Wow, what a pain! Let's hope MS puts in some VS design-time support for x:Bind.
We to be able to use the VS designer but also be able to switch easily to x:Bind instead of Binding. Here's what I did:
In my View, I added a property to get my ViewModel. This makes sense because x:Bind paths are relative to the Page (i.e. the View object).
In my Page XAML, I added the following to the <Page ... > at the top of the XAML:
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=local:MyView, IsDesignTimeCreatable=False}"
DataContext="{x:Bind}"
This way, the Page's actual data context is set to the Page itself due to the {x:Bind}. That's because x:Bind is relative to the Page and there is no path given.
At the same time, due to the d:DataContext line, the VS designer reflects on the MyView class (without creating an instance) for the purpose of the VS designer interaction. This lets VS design from MyView, where you can then scroll down to the ViewModel property, expand it and select the item that you want to bind to.
When you do all that, the VS designer will create a Binding statement whose path is relative to the View, i.e. it happens to be exactly the same as the path that x:Bind expects. So, if you want to switch to x:Bind later on, you can just search and replace all "{Binding" with "{x:Bind".
Why do we even need the d:DataContext line to tell VS what class to look at? Good question, since you would think that VS could figure out the very next line sets the DataContext to the Page, using DataContext={x:Bind}. Go ahead and try it, it does not work and neither does it work if you change x:Bind to Binding relative to self.
Hopefully this situation will get cleaned up by MS !!
If you are not tooo fussy on the data have a look at the sample data found in xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
You use it like this...
<ItemsControl ItemsSource="{Binding Path=Report.Audit.Data}" d:ItemsSource="{d:SampleData}" Grid.Row="1">
<ItemsControl.ItemTemplate>
<DataTemplate>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
it then renders the items control with a few rows of data
I've solved the problem adding d:DataContext="{d:SampleData}" in the component definition (UserControl or Window).
<UserControl x:Class="TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TestApp.Views"
DataContext="{Binding TestViewModel}"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
d:DataContext="{d:SampleData}"
>