473,410 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

how to create WPF User Control?

how to create WPF User Control?
Oct 5 '15 #1
1 4407
Sherin
77 64KB
the XAML code in which a button and a text box is created with some properties in MyUserControl.xaml file.
Expand|Select|Wrap|Line Numbers
  1. <UserControl x:Class = "WPFUserControl.MyUserControl" 
  2.    xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.    xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
  4.    xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"  
  5.    xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"  
  6.    mc:Ignorable = "d" d:DesignHeight = "300" d:DesignWidth = "300"> 
  7.  
  8.    <Grid> 
  9.       <TextBox Height = "23"  
  10.          HorizontalAlignment = "Left"  
  11.          Margin = "80,49,0,0" Name = "txtBox"  
  12.          VerticalAlignment = "Top" Width = "200" /> 
  13.  
  14.       <Button Content = "Click Me"  
  15.          Height = "23" HorizontalAlignment = "Left"  
  16.          Margin = "96,88,0,0" Name = "button"  
  17.          VerticalAlignment = "Top" Click = "button_Click" />    
  18.    </Grid>
  19.  
  20. </UserControl>
Given below is the C# code for button click event in MyUserControl.cs file which updates the text box.
Expand|Select|Wrap|Line Numbers
  1. using System; 
  2. using System.Windows; 
  3. using System.Windows.Controls; 
  4.  
  5. namespace WPFUserControl {
  6.    /// <summary>
  7.       /// Interaction logic for MyUserControl.xaml 
  8.    /// </summary> 
  9.  
  10.    public partial class MyUserControl : UserControl { 
  11.  
  12.       public MyUserControl() { 
  13.          InitializeComponent(); 
  14.       }  
  15.  
  16.       private void button_Click(object sender, RoutedEventArgs e) { 
  17.          txtBox.Text = "You have just clicked the button"; 
  18.       } 
  19.    } 
  20. }
Here is the implementation in MainWindow.xaml to add the user control.

Expand|Select|Wrap|Line Numbers
  1. <Window x:Class = "XAMLUserControl.MainWindow" 
  2.    xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.    xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
  4.    xmlns:control = "clr-namespace:WPFUserControl" 
  5.    Title = "MainWindow" Height = "350" Width = "525"> 
  6.  
  7.    <Grid> 
  8.       <control:MyUserControl/> 
  9.    </Grid> 
  10.  
  11. </Window> 
  12.  
May 21 '20 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: roni | last post by:
hi. i want to build USER CONTROL that will be used in WEB PAGE. to build user control and use in web page,I KNOW . i did user control with textbox and lable for example and it works. the...
1
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a...
0
by: Aaron | last post by:
Is it possible to add my own fields to the create user control if so can i store all user fields into my own db? instead of the default fields i want to have a field for address, state, zip and...
0
by: Gujju | last post by:
Hi all, Have a question from the following post to create user control in a Datagrid.... http://www.dotnet247.com/247reference/msgs/45/225337.aspx I have a question.... I can create the...
2
by: Ellis Yu | last post by:
Hi all, Thanks for Jim M and Herfried K Wagner first. I'd posted a question here about building dll output file in standand edition. It works after changing outputtype to "library". And now I...
3
by: Brian | last post by:
Hi, All, I want to create a single user control (component) with multi-controls. for example, I want to use one button control and 2 listBox controls to build one single user control. so, user...
1
by: rushikesh.joshi | last post by:
Hi All, I want some charting functionality in my ASP.NET application. I want to show a multiple bar on my web page. It's based on down time of different servers. like server1: down betn 4 AM...
1
by: vasu | last post by:
how to create user control in asp.net
2
by: Roman | last post by:
I would like to slightly change the functionality of the create user control. 1. When creating a user the password shouldn't be supplied. Rather the password should be generated much like...
2
by: Gugale at Lincoln | last post by:
Hi, I am creating a user control which has a radio button and few other controls. I would like the user control to behave like a radio button, in the sense when added to a container radio button...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.