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.
Related
I am trying implementing an addin for SparxEA with the MVVM Light. One thing that I found interesting is seeing “live” data in a window as is mentioned in the course of MVVM Light. So, I would like to do the same. As I have Class Library project I can’t use App.XAML.
In XAML I have this code:
<Window x:Class="GoatJira.View.About"
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:GoatJira.View"
xmlns:viewmodel="clr-namespace:GoatJira.ViewModel"
mc:Ignorable="d"
Title="{Binding Path=AboutTitle}" Height="322.613" Width="573.608" ResizeMode="NoResize" ShowInTaskbar="False" WindowStartupLocation="CenterScreen" Initialized="Window_Initialized"
DataContext="{Binding Source={StaticResource ResourceKey=AboutData}}"
>
<Window.Resources>
<ResourceDictionary>
<viewmodel:AboutViewModel x:Key="AboutData"/>
</ResourceDictionary>
</Window.Resources>
…
This perfectly works within Visual Studio IDE. When I run the app and want to instantiate the window, I obtain this exception (translated from Czech): Source marked as AboutData was not found. Names of sources are case sensitive.
When I remove the 10th line with DataContext, I can’t see the bind data within the VS, on the other hand, application works fine when I assign the DataContext in code. My understanding is, that there is a way when it works in VS and in running app without changing anything.
Do you have any idea what I am doing wrong?
If needed, the whole code is at https://github.com/SlavekRydval/GoatJira.
What happens if you move the DataContext to after the resource section i.e.
<Window blah=for>
<Window.Resources>
<ResourceDictionary>
<viewmodel:AboutViewModel x:Key="AboutData"/>
</ResourceDictionary>
</Window.Resources>
<Window.DataContext>
<StaticResourceExtension ResourceKey="AboutData"/>
</Window.DataContext>
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 :-)
I´m using the librariay Extended WPF Toolkit
https://wpftoolkit.codeplex.com/
Is it possible to resize a ChildWindow like a ordinary Window?
The documentation shows some properties that seem to be related, but they are not accessible via xaml. https://wpftoolkit.codeplex.com/wikipage?title=ChildWindow
This is the example that I´m trying:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock"
xmlns:s="clr-namespace:System;assembly=mscorlib">
<xctk:WindowContainer>
<xctk:ChildWindow WindowBackground="Blue"
Left="75"
Top="50"
Width="275"
Height="125"
WindowState="Open" Canvas.Top="52">
<TextBlock Text="This is a Child Window" Padding="10"/>
</xctk:ChildWindow>
</xctk:WindowContainer>
</Window>
It appears as though the Resize functionality is only available in the Plus Edition of the software, the documentation can be found at the link below.
Based on the link you provided, it looks like you are using the same version as I am, which is the free one.
https://wpftoolkit.codeplex.com/wikipage?title=ChildWindow%20Plus
Kind of disappointing, we don't utilize the toolkit enough to warrant the cost.
In my wpf project, I have added two resource files:
Resources\English.resx and Resources\German.resx
in MainWindow.xml, I try to find the value from the resource file:
<Window x:Uid="Window_1" x:Class="LocalizationInvestigate.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Resources="clr-namespace:LocalizationInvestigate.Resources"
Title="MainWindow" Height="350" Width="525">
<Grid x:Uid="Grid_1">
<Label x:Uid="Label_1" Content="{x:Static Resources:English.LabelHello}"></Label>
</Grid>
</Window>
For English, it works perfectly this way. However, based on the local language, how I can make it automatically switch to German by using: Resource:German.LabelHello?
Well usually, you would create resource files with the standard culture string in its name. E.g.
Strings.en.resx
Strings.en-US.resx
Strings.de-DE.resx
Strings.de-AU.resx
...
The resource manager would switch the culture according to the Thread.CurrentUICulture. I think this is good article about it. The localization has also a fallback behavior, so that unknown cultures would be answered with the en resources.
The usage in the XAML would be.
<Label Content="{x:Static Resources:Strings.LabelHello}" />
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}"
>