Using a class that inherits System.Windows.Controls.Control inside XAML - wpf

I have wpf application with the following xaml as the mainwindow
<Window x:Class="Video_Editor.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">
<Grid>
</Grid>
</Window>
I also have a class that inherits 'Control' called 'MyControl'.
How do I make it possible to put an instance of that MyControl inside the xaml.
Something like this
<Grid>
<MyControl/>
</Grid>

You need to setup the XAML namespace mapping:
<Window x:Class="Video_Editor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:YourNamespaceContainingMyControl"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:MyControl />
</Grid>
</Window>
Note that, if the control is in a different assembly (DLL), you'll need to use xmlns:local="clr-namespace:YourNamespaceContainingMyControl;assembly=YourLibrary".

Related

Attaching CollectionViewType to a CollectionViewSource

This might be simple but I do not know how to go about it. I want to attach a CollectionViewType like this: CollectionViewType="ListCollectionView" to a CollectionViewSource. I am using a typed DataSet and the CollectionViewSource was generated through a Drag and Drop. I am using a DataSet and not Entity Framework.
I have put the following references:
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
However, I am failing to modify the CollectionViewSorce accordingly.
<Window x:Class="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:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow"
Width="500"
Height="400"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
>
<Window.Resources>
<local:DataSet1 x:Key="DataSet1" />
<CollectionViewSource x:Key="ExpenseDescriptionsViewSource" Source="{Binding ExpenseDescriptions, Source={StaticResource DataSet1}}" />
</Window.Resources>
<Grid >
</Grid>
</Window>
All the examples I have seen deal with Entity Framework and not with DataSet.
How can I implement CollectionViewType changes in the CollectionViewSource above using DataSset?

Use user control in xaml

I think I'm missing something very obvious.
I create a WPF application and a user control. Both within one project in Visual Studio. In the WPF application I want to use the user control but the compiler pretends not to know the user control:
Error 1 UserControl1 is not supported in a Windows Presentation Foundation (WPF) project
Here is the WPF application XAML:
<Window x:Class="WpfApplication1.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">
<Grid>
<UserControl1></UserControl1>
</Grid>
And this is the XAML code of my control
<UserControl x:Class="WpfApplication1.UserControl1"
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="300">
<Label>Hello World</Label>
You have missed declaration of the namespace in your xaml:
<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"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:UserControl1></local:UserControl1>
</Grid>
</Window>

How to add a page in another dll into a window in wpf

I created a new wpf application, with the default MainWindow.xaml,
and I created a new page: Page1.xaml with a button in it.
I want to embed the page in the window, so I tried:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication1="clr-namespace:WpfApplication1" Title="MainWindow" Height="350" Width="525">
<Grid>
<WpfApplication1:Page1></WpfApplication1:Page1>
</Grid>
</Window>
Then I got a exception.
I searched here, and got another solution, using
so I tried this:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication1="clr-namespace:WpfApplication1" Title="MainWindow" Height="350" Width="525">
<Grid>
<Frame Source="Page1.xaml"/>
</Grid>
</Window>
it worked.
But
what if the Page1.xaml is not in current project but in a dll file?
The general template to get resources from another assembly is : Source="pack://application:,,,/YourAssembly;component/Subfolder/YourResourceFile.xaml"
Take a look a this on MSDN

Moving a part of xaml file to another xaml file

In my project, I have XAML file like this:
<Grid Margin="50,0,0,0">
//Huge amount of code goes here
</Grid>
It is very difficult to go through all the code in Grid while designing. Can I move all the code to a separate XAML file file and in in this Grid content i will call that XAML file??
<Grid Margin="50,0,0,0">
//call xaml file here
</Grid>
You should define a UserControl; assume the "Huge amount of code" is sth like this:
<Border Name="yourBorder">
//Other Xamls
</Border>
Now you create a new UserControl an put this Border in it
<UserControl x:Class="WpfApplication.UserControl1"
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="300">
<Border Name="yourBorder">
//Other Xamls
</Border>
</UserControl>
You can use UserControl1 in other Xamls. You should add xmlns:wp="clr-namespace:WpfApplication" to your Xaml. For example if you want to use it in a Window:
<Window x:Class="WpfApplication.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wp="clr-namespace:WpfApplicationUpper" //This is what I mentioned
Title="Window2" Height="300" Width="300">
<StackPanel>
<wp:UserControl1 /> // You call it using this format
</SatckPanel>

Add a user control to a wpf window

I have a user control that I've created, however when I go to add it to the XAML in the window, Intellisense doesn't pick it up, and I can't figure out how to add it to the window.
You need to add a reference inside the window tag. Something like:
xmlns:controls="clr-namespace:YourCustomNamespace.Controls;assembly=YourAssemblyName"
(When you add xmlns:controls=" intellisense should kick in to make this bit easier)
Then you can add the control with:
<controls:CustomControlClassName ..... />
You probably need to add the namespace:
<Window x:Class="UserControlTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:UserControlTest"
Title="User Control Test" Height="300" Width="300">
<local:UserControl1 />
</Window>
Make sure there is an namespace definition (xmlns) for the namespace your control belong to.
xmlns:myControls="clr-namespace:YourCustomNamespace.Controls;assembly=YourAssemblyName"
<myControls:thecontrol/>
This is how I got it to work:
User Control WPF
<UserControl x:Class="App.ProcessView"
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="300">
<Grid>
</Grid>
</UserControl>
User Control C#
namespace App {
/// <summary>
/// Interaction logic for ProcessView.xaml
/// </summary>
public partial class ProcessView : UserControl // My custom User Control
{
public ProcessView()
{
InitializeComponent();
}
} }
MainWindow WPF
<Window x:Name="RootWindow" x:Class="App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:app="clr-namespace:App"
Title="Some Title" Height="350" Width="525" Closing="Window_Closing_1" Icon="bouncer.ico">
<Window.Resources>
<app:DateConverter x:Key="dateConverter"/>
</Window.Resources>
<Grid>
<ListView x:Name="listView" >
<ListView.ItemTemplate>
<DataTemplate>
<app:ProcessView />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Window>

Resources