Referencing a User Control in a WPF Application

Currently I am working on a WPF Tool which gets from Database and Excel onto a grid view, modifies it and saves it , I created a User control so as to use in my app . I wanted to use this usercontrol in my Mainwindow.xaml view ... But it was as easy as we do in asp.net....

Consider I have a Usercontrol which i added in my project as follows 

<UserControl x:Class="Hoverhelpinputter.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="385" Height="541" Width="927">
    <Grid Width="889" Height="545">
       
        <DataGrid  SelectionChanged="DataGrid2_SelectionChanged" AutoGenerateColumns="True" ItemsSource="{Binding}" Name="DataGrid2" Margin="0,55,0,12" Grid.ColumnSpan="3" Grid.RowSpan="4">
           
        </DataGrid>

    </Grid>
</UserControl>

and than I have my Mainwindow.xaml view as follows

<Window xmlns:my="clr-namespace:Smith.WPF.HtmlEditor;assembly=Smith.WPF.HtmlEditor"
        x:Class="Hoverhelpinputter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"  Height="686" Width="1094">

<Grid Width="994">

 </Grid>

</Window>

 xmlns:local="clr-namespace:Hoverhelpinputter"   

The xmlns:local attribute of the Window element, maps the xml namespace localto the Hoverhelpinputter namespace. This is the syntax:

xmlns:{name}="clr-namespace:{namespace};assembly={assembly}"
so add this attribute in the mainwindow.xaml view as below and call it  from the mainwindow through   <local:UserControl1 />


<Window xmlns:my="clr-namespace:Smith.WPF.HtmlEditor;assembly=Smith.WPF.HtmlEditor"
        xmlns:local="clr-namespace:Hoverhelpinputter" 
        x:Class="Hoverhelpinputter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"  Height="686" Width="1094">

<Grid Width="994">

<local:UserControl1 />

 </Grid>

</Window>

No comments:

Post a Comment