Hi Frnds,
In the last post I explained of creating a user control which acts like a container. In this post I will explain how to change the properties of your user control. The following steps will clear you about this,
1. Create a public property for the user control, accessing on which you will change the properties of your usercontrol. This is something like,
Private string someText;
Public string SomeText
{
Set { this.txtName.Text = value; }
Get { return txtName.Text; }
}
2. Since your properties are kept inside control template you cannot refer them simply by name as above.i.e.,this.txtName is not accessible if txtName is name of an element inside the control template.To access that element you need to have something like,
TextBlock txtBlock = (TextBlock)Template.FindName(“txtName”,this);
So if you think of something like,
Set
{
TextBlock txtBlock = (TextBlock)Template.FindName(“txtName”,this);
txtBlock.Text = value;
}
Then you are mistaken. This is because inside the setter you cannot fill the object txtBlock. This means that you will get a null reference when you run the above code. The reason is control hierarchy loading. When your setter method is executing your TextBlock is not yet created and so it will return an error(a run-time error).So the solution would be to set the value inside onApplyTemplate().The overall code looks like this:
MyUserControl.xaml
<USerControl x:Class="MyUserControl" Height="Auto" Width="Auto"......>
<UserControl.Template>
<ControlTemplate>
<Grid>
<Border Height="Auto" Width="Auto" Background="{TemplateBinding Property=ContentControl.Background}" CornerRadius="10" Name=”myContainer”>
<ContentPresenter Content="{TemplateBinding Property=ControlContent.Content}" />
</Border>
</Grid>
</ControlTemplate>
</UserControl.Template>
MyUserControl.xaml.cs
Public partial class MyUserControl : UserControl
{
Public MyUserControl()
{
InitializeComponent();
}
Public override void OnApplyTemplate()
{
Border border = (Border)Template.FindName(“myContainer”,this);
border.CornerRadius = new CornerRadius(topLeftRadius,0,10,0);
base.OnApplyTemplate();
}
Private double topLeftRadius;
Public double TopLeftRadius
{
Set { topLeftRadius = value; }
Get { return topLeftRadius; }
}
}
And in the main window Main.xaml
<Window…….>
<MyUserControl Background=”Chocolate” Height=”100” Width=”65” TopLeftRadius=”20”>
<TextBlock HorizontalAlignment=”Center” VerticalAlignment=”Center”>Some Text </TextBlock>
</MyUserControl>
</Window>
Please feel free to scold me if anything wrong mentioned.
Hope this was helpful
April 13, 2009
Accessing template elements of a usercontrol
0 comments 4/13/2009 03:13:00 PM Posted by kalyan kumar BApril 01, 2009
Creating UserControl to hold other controls in WPF
4 comments 4/01/2009 07:42:00 PM Posted by kalyan kumar BHi Friends,
You can find many samples in google for creating user controls and my post stands little adhead of them.
Firstly if you create a user control and in the main window where you use the usercontrol, if you try to put some content within your usercontrol, it will throw an error. This is because your usercontrol is not designed to handle other wpf elements within it. To make this happen you have to follow a different apporach.
The following is a sample code for creating such a control:
<UserControl.Template>
<ControlTemplate>
<Grid>
<Border Height="Auto" Width="Auto" Background="{TemplateBinding Property=ContentControl.Background}" CornerRadius="10">
<ContentPresenter Content="{TemplateBinding Property=ControlContent.Content}" />
</Border>
</Grid>
</ControlTemplate>
</UserControl.Template>
And in your main window just add it as
<MyUserControl Background="LightBlue" Height="50" Width="75">
<Button content="Push Me">
</MyUserControl>
Remember here both MyUserControl and the Main window remain in the same namespace. The result of the above code will be a rounded rectangle with a button inside it. Creating a rounded rectangle as an usercontrol is easy but it will not act like a container for holding for other elements. What the magic i have done here is, i've defined the look of my usercontrol as rounded rectangle inside the usercontrol template and mainly the ContentPresenter Tag which makes the control to hold other items. Other important issue to look at is the TemplateBinding. I will not explain about TemplateBinding for now but you can find many sites that will help you around.
In my next post i'll explain how to change/access the cornerradius of the rectangle from code-behind of the usercontrol which will help you to expose the accessibility to the main window.
March 24, 2009
Double-click solution file to create your web site
0 comments 3/24/2009 02:20:00 PM Posted by kalyan kumar BLabels: IIS, Web Application
Hi all,
In Visual Studio, if you have to go for any web based content you will be left with two choices - web site/web application. Here is one difference that may be helpful for your selection criteria:
- Create a solution file.
- Add a new project and select the project type as "Asp.Net Web Application".
- Right click on this web application and select properties - go to 'Web' tab, under server you will find the radio box 'Use Visual Studio Development Server' selected. Below that is the radio box 'Use Local IIS Web server'. Uncheck the former and select the later one.
- Now when you export your solution file to another machine and when you double-click the solution file, your web site is automatically created in the IIS Server of that machine.
March 13, 2009
Difference between Raster Image and Vector Based Images
0 comments 3/13/2009 03:29:00 PM Posted by kalyan kumar BHi All,
In differentiating the two kinds of images what i conclude is "Vector based images are mathematical images - this implies that these are image format used by computers. This is true because a raster image has definition defined pixel by pixel.
For a better understanding pls go through the following link:
http://www.nw-media.com/photography-articles/31/raster-vs-vector/
March 03, 2009
ADO.NET Entity Framework and Linq Framework
0 comments 3/03/2009 02:48:00 PM Posted by kalyan kumar BI googled for this and i have found the following links which will clear many confusions
February 19, 2009
Writing Your Own HttpHandler
0 comments 2/19/2009 03:01:00 PM Posted by kalyan kumar BHi All,
February 16, 2009
Improve u r english
0 comments 2/16/2009 01:07:00 PM Posted by kalyan kumar BHi Friends,