Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

Thursday, February 9, 2012

Where do .Net ClickOnce(i.e. WPF)) application get deployed?

Where do .Net ClickOnce(i.e. WPF)) application get deployed?

NET ClickOnce applications (i.e. WPF) get deployed into a subfolder of C:\Documents and Settings\\Local Settings\Apps\2.0.

Monday, October 3, 2011

WPF- How to find the height/width of a WPF control added dynamically

//Dynamically cretaed TextBlock
TextBlock objTextBlock = new TextBlock();
objTextBlock.Loaded += new RoutedEventHandler(objTextBlock_Loaded);

//Access the dimensions using ActualHeight and ActualWidth in this event.

void
objTextBlock_Loaded(object sender, RoutedEventArgs e)
{
TextBlock objTextBlock = sender as TextBlock;


MessageBox.Show(objTextBlock.ActualHeight.ToString());

MessageBox.Show(objTextBlock.ActualWidth.ToString());

}

Tuesday, September 6, 2011

Timeout issue in your App

First thing to check here is optimize your queries and then we can set the timeout property of command object (thats excuting the

stored procedure) ,not the timeout property of the connection string(which specify how long you want to try to establish a database connection)

//try following in your code
command.CommandTimeout = connection.ConnectionTimeout;

Saturday, September 3, 2011

The Initialized event and Loaded event in WPF

WPF controls has both Initialize and Loaded events. Initializing and loading a control tend to happen at about the same time, and consequently these events fire at roughly the same time.

The Initialized event says just that an element has been created and its properties have all been set, and as a consequence this usually fires on children before their parent. So when Initialized is raised on an element, its whole sub-tree is likely initialized, but its parent is not. The Initialized event is typically fired when the Xaml for a sub-tree is loaded. This event corresponds to the IsInitialized property.

The Loaded event says that the tree is not only built and initialized, but layout has run on it, data has been bound, it's connected to a rendering surface (window), and you're on the verge of being rendered. When we reach that point, we canvas the tree by broadcasting the Loaded event, starting at the root. This event corresponds to the IsLoaded property.



Example: in case of a TextBox inside a Page:

  • TextBox.IsInitialized goes true
  • TextBox.Initialized event is raised
  • Page.IsInitialized goes true
  • Page.Initialized event is raised
  • Page IsLoaded goes to true
  • TextBox IsLoaded goes to true
  • Page.Loaded is raised
  • TextBox.Loaded is raised

Saturday, August 6, 2011

WPF-Basic ComboBox Example

XBAP:

<ComboBox Name="MyComboBox" Grid.Column="1" Grid.Row="2">

</ComboBox>

Code Behind:

DataTable displayformat = GetNameList();

if (displayformat.Rows.Count > 0)

{ Binding displayBinding = new Binding();

displayBinding.Source = displayformat;

MyComboBox.SetBinding(ComboBox.ItemsSourceProperty, displayBinding);

MyComboBox.DisplayMemberPath = "Name";

MyComboBox.SelectedValuePath = "ID";

}

WPF - Checking Cap Lock Status in WPF

Checking Cap Lock status will be useful in Logon page where we can provide warning to user <Caps Lock is on. Having Caps Lock on may cause you to enter password incorrectly.>

Following sample uses the Control class that is a standard class within the System.Windows.Forms namespace. The DLL containing this namespace is automatically included in Windows Forms applications. The class includes a method named IsKeyLocked, which allows you to determine whether keys such as Caps Lock are switched on or off. To check the status of the Caps Lock key, you can use the method in the following manner:

Example:
private void KeyDownEventHanlder(object sender, KeyEventArgs e)
{
if (Console.CapsLock == true)
{
lblError.Foreground =
Brushes.Red;
lblError.Content = "Caps Lock is on.";// Having Caps Lock on may cause you to enter password incorrectly.";

}

else

{
if (lblError.Foreground == Brushes.Red)lblError.Foreground = Brushes.Transparent;
}

}

Wednesday, July 27, 2011

WPF- Retreive Parent TreeView Node Item from Child node

To retreive parent tree view item from child nodes try following:

TreeViewItem parent = (tree.SelectedItem as TreeViewItem).Parent as TreeViewItem;

int iValue = parent.ItemID;

Tuesday, July 26, 2011

WPF-Customized Menu

The coolest thing which I like about WPF is that we can cutomize its controls as per our needs.

Following XAML is generating a Menu with an Image

<MenuItem Name="mnuItemAlert" HorizontalAlignment="Left" FontFamily="Courier" FontSize="14" Header="Alerts" FlowDirection="LeftToRight">

<MenuItem.Icon>

<Image HorizontalAlignment="Left" Width="20" Height="25" Visibility="Visible" Source="Images/alert.gif" />

</MenuItem.Icon>

</MenuItem>

Now we can modify this XAML as per our need(by adding stack panel image and text appears much closer...)

<MenuItem Name="mnuItemAlert" HorizontalAlignment="Left" MouseEnter="mnuItemAlert_MouseEnter" FontFamily="Courier" FontSize="14" FlowDirection="LeftToRight">

<MenuItem.Header>

<StackPanel Orientation="Horizontal">



<TextBlock Margin="3" >Alerts</TextBlock>

<Image HorizontalAlignment="Left" Width="20" Height="25" Visibility="Visible" Source="Images/alert.gif" />



</StackPanel>

</MenuItem.Header>

</MenuItem>

Wednesday, July 20, 2011

TextBox Control to accpet numeric values


Following sample shows WPF TextBox which accept only numeric values.

XAML File:

<TextBox Background="#FFF1F8FE" Name="tStudy" PreviewTextInput="TextBox_NymericInput" ></TextBox>



CodeBehind:

private void TextBox_NymericInput(object sender, System.Windows.Input.TextCompositionEventArgs e)

{

e.Handled = !AreAllValidNumericChars(e.Text);

}

bool AreAllValidNumericChars(string str)

{

bool ret = true;

int l = str.Length;

for (int i = 0; i < l; i++)

{

char ch = str[i];

ret &= Char.IsDigit(ch);

}
return ret;
}

Monday, July 18, 2011

WPF- XBAP page unload event


Following code explain how to use page unload event in your XBAP pages.

--XAML

<Page x:Class="MyTestPage"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="MyTestPage" Unloaded="Page_Unloaded">



--Code Behind
private void Page_Unloaded(object sender, RoutedEventArgs e)
{

// Your code here


}

WPF-Combo Box Binding example

Following sample will show how to databind in WPF ComboBox

Following code goes into your xaml file

----------------------------------------------------------------------------------------
<ComboBox Name="comboBox1" Grid.Row="1" Grid.Column="2" SelectionChanged="comboBox1_DropDownClosed"
DataContext="Binding ElemntName=LanguageListBinding,Path=SelectedItem" FontSize="13" Height="20" >

<ComboBox.ItemTemplate>

<DataTemplate>

<StackPanel Name="spSitePanel" Orientation="Horizontal">
<TextBlock Visibility="Hidden" Width="0" x:Name="txtSiteID" Text="{Binding Path=LanguageID}" />
<TextBlock x:Name="txtSiteName" Text="{Binding Path=Language}" />



</StackPanel>

</DataTemplate>

</ComboBox.ItemTemplate>
</ComboBox>
---------------------------------------------------------------------------------------------------------

following code goes into codebehind of xaml file (cs file)

public static localhost1.MyClass g_ProxyWebService;
g_ProxyWebService = new localhost1.MyClass();

... ......... .......... ...........

... ......... .......... ...........
Binding LanguageListBinding = new Binding();localhost1.clsUser[] objProtocol = g_ProxyWebService.GetLanguageList();
LanguageListBinding.Source = objProtocol;

comboBox1.SetBinding(ComboBox.ItemsSourceProperty, LanguageListBinding);
comboBox1.SelectedIndex = 0;



----------------------------------------------------------------------------------------------------

Wednesday, July 13, 2011

WPF- Using hyperlinks in XBAP application

Following XAML can be used for hyperlinks in XBAP application

<TextBlock>

<Hyperlink NavigateUri="http://microsoft.com" TargetName="_blank">
http://microsoft.com


</Hyperlink>
</TextBlock>

Monday, July 11, 2011

WPF-how to set label font to Bold from CodeBehind

Label tb = new Label();
tb.Name = "LabelID" + iRow;
tb.Content = lblName;

Grid.SetColumn(tb, iCol);

Grid.SetRow(tb, iRow);
Grid.SetColumnSpan(tb, 2);
tb.Foreground = Brushes.DarkGreen;

tb.Height = 50;

tb.FontSize = 18;

tb.FontWeight = FontWeights.Bold;


gChildGrid.Children.Add(tb);

Unable to debug in VS2010

I was getting following message while running my VS2010 in debug mode:

"Unable to automatically step into the server. Unable to determine a stopping location. Verify symbols are loaded. Symbol not found: Service.HelloWorld()."

While doing more investigation I did find that my Visual studio was unable to attach to the aspnet_wp.exe when I hit
the start debug button. After doing some reserach I was able to find the issue. My webservice was upgraded to use .Net 4.0 but WPF code was still using .NET 3.5. After changing my WPF to .NET 4.0 issue was fixed.


you can look at other articles also:

Debug ASP.net web services http://msdn.microsoft.com/en-us/library/aa291236(VS.71).aspx

Enable debugging for ASP.NET application http://msdn.microsoft.com/en-us/library/e8z01xdh

WPF- How to Put WPF tab Control on left hand side with Tab Header rotated to 270

Following sample will show how we can put tab control on left hand side with tab header rotated to 270

<TabControl TabStripPlacement="Left">

<TabControl.Resources>

<Style TargetType="{x:Type TabItem}">

<Setter Property="Padding" Value="8" />

<Setter Property="HeaderTemplate">

<Setter.Value>

<DataTemplate>

<ContentPresenter Content="{TemplateBinding Content}">

<ContentPresenter.LayoutTransform>

<RotateTransform Angle="270" />

</ContentPresenter.LayoutTransform>

</ContentPresenter>

</DataTemplate>

</Setter.Value>

</Setter>

</Style>

</TabControl.Resources>

<TabItem Header="My Tab Item 1" />

<TabItem Header="My Tab Item 2" />

<TabItem Header="My Tab Item 3" />

<TabItem Header="My Tab Item 4" />

</TabControl>

WPF- Margin and Padding

The Margin and Padding properties can be used to reserve some space around of within the control.

•The Margin is the extra space around the control.
•The Padding is extra space inside the control.
•The Padding of an outer control is the Margin of an inner control.

WPF-Merged Resource Dictionaries

Windows Presentation Foundation (WPF) resources support a merged resource dictionary feature. This feature provides a way to define the resources portion of a WPF application outside of the compiled XAML application. Resources can then be shared across applications and are also more conveniently isolated for localization.


Example:

<Page.Resources>
<ResourceDictionary>

<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resource\Resource1.xaml"></ResourceDictionary>
<ResourceDictionary Source="Resource\Resource2.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>

WPF-How to create certificate for your XBAP application

If we have to run our XBAP application in fulltrust mode we need to create certificate which has to be added to client machine.

There are 2 ways to get certficate:

1. Get it from certified authority i.e. http://verisign.com

2. Generate a test signing certificate using Visual Studio ( Project Properties, Signing, "Create Test Certificate"

user still need to download and install certificate on there machine. This process cannot be automated.

May be a script can be created which calls certutil.exe utility

Friday, July 8, 2011

WPF-Design guidelines for WPF published

Microsoft has published design guidelines for WPF application on MSDN.

Here is the link:

http://msdn2.microsoft.com/en-us/library/aa511329.aspx</font

WPF-Creating controls dynamically in WPF

Couple of days I wrote about creating ASP.Net controls dynamically in C#, following code explain how to create in WPF (using c#).



using System;

using System.Collections.Generic;

using System.Data;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Controls.Primitives;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Media.Animation;

using System.Windows.Navigation;

using System.Windows.Shapes;
namespace CompleWebCRF.Test
{

/// <summary>

/// Interaction logic for dynamicControl.xaml

/// </summary>

public partial class dynamicControl : Page

{
public dynamicControl()
{

InitializeComponent();



test();

}




private void test()
{
int iRow=0;int iCol = 0;
myGrid.RowDefinitions.Clear();
createLabel("Text Box", iRow, iCol);
CreateTextBox(iRow, iCol+1);



iRow++;
createLabel("List Box", iRow, iCol);
CreateListBox(iRow, iCol+1);



iRow++;
createLabel("Combo Box", iRow, iCol);
CreateComboBox(iRow, iCol+1);

iRow++;
createLabel("Check Box", iRow, iCol);
CreateCheckBox(iRow, iCol+1);

iRow++;
createLabel("Radio Button", iRow, iCol);
CreateRadioButton(iRow, iCol+1);

CreateRadioButton(iRow, iCol+2);

iRow++;

CreateButton(iRow, iCol);





}
private void createLabel(string lblName, int iRow, int iCol)
{
myGrid.RowDefinitions.Add(new RowDefinition());
myGrid.ColumnDefinitions.Add(new ColumnDefinition());
Label tb = new Label();tb.Name = "LabelID" + iRow;
tb.Content = lblName;

Grid.SetColumn(tb, iCol);
Grid.SetRow(tb, iRow);


tb.Width = 200;

tb.Height = 50;

myGrid.Children.Add(tb);

}
private void CreateButton(int iRow,int iCol)
{
myGrid.RowDefinitions.Add(new RowDefinition());Button b = new Button();


b.Click += Button_Click;

b.Width = 70;

b.Height=30;


b.Content = "Save";

Grid.SetColumn(b, iCol);Grid.SetRow(b, iRow);
myGrid.Children.Add(b);

}




private void Button_Click(object sender, RoutedEventArgs e)
{
foreach (Control c in myGrid.Children)
{
if (c is TextBox)
{

TextBox txtResponse = c as TextBox;
MessageBox.Show("TextBox -- Name: " + txtResponse.Name.ToString() + " ,Value: " + txtResponse.Text.ToString());
}
if (c is CheckBox)
{

CheckBox objCheckBox = c as CheckBox;
MessageBox.Show("CheckBox -- Name: " + objCheckBox.Name.ToString()+ " ,Item Selected: " + objCheckBox.IsChecked.ToString() );
}
if (c is ComboBox)
{
ComboBox objComboBox = c as ComboBox;
if (objComboBox.SelectedValue==null)
MessageBox.Show("ComboBox--Name: " + objComboBox.Name.ToString() + " ,Item Selected: " + objComboBox.SelectedIndex.ToString() + " ,Value Selected: Null " );
else
MessageBox.Show("ComboBox--Name: " + objComboBox.Name.ToString() + " ,Item Selected: " + objComboBox.SelectedIndex.ToString() + " ,Value Selected: " + objComboBox.SelectedValue.ToString());
}
if (c is ListBox)
{
ListBox objListBox = c as ListBox;
if (objListBox.SelectedValue == null)
MessageBox.Show("ListBox--Name: " + objListBox.Name.ToString() + " ,Item Selected: " + objListBox.SelectedIndex.ToString() + " ,Value Selected: Null ");
else
MessageBox.Show("ListBox--Name: " + objListBox.Name.ToString() + " ,Item Selected: " + objListBox.SelectedIndex.ToString() + " ,Value Selected: " + objListBox.SelectedValue.ToString());
}


if (c is RadioButton)
{
RadioButton objRadioButton = c as RadioButton;
//if (objRadioButton.SelectedValue == null)

// MessageBox.Show("Radio Button--Name: " + objRadioButton.Name.ToString() + " ,Item Selected: " + objRadioButton.SelectedIndex.ToString() + " ,Value Selected: Null ");

//else

// MessageBox.Show("Radio Button--Name: " + objRadioButton.Name.ToString() + " ,Item Selected: " + objRadioButton.SelectedIndex.ToString() + " ,Value Selected: " + objRadioButton.SelectedValue.ToString());
String strGroupName = objRadioButton.GroupName;
MessageBox.Show(strGroupName.ToString());
MessageBox.Show(objRadioButton.IsChecked.ToString());
//lstBox.GroupName



}



}

}
private void CreateTextBox(int iRow, int iCol)
{
myGrid.RowDefinitions.Add(new RowDefinition());
myGrid.ColumnDefinitions.Add(new ColumnDefinition());

TextBox tb = new TextBox();
tb.Name = "textBox" + iRow;


Grid.SetColumn(tb, iCol);
Grid.SetRow(tb, iRow);
tb.TextWrapping = TextWrapping.Wrap;tb.Text =iRow+
"When we enter value, they should be entered here.";
tb.Width=200;

tb.Height = 50;



myGrid.Children.Add(tb);

}
private void CreateListBox(int iRow, int iCol)
{

myGrid.RowDefinitions.Add(new RowDefinition());
myGrid.ColumnDefinitions.Add(new ColumnDefinition());


ListBox lstBox = new ListBox();
Grid.SetColumn(lstBox, iCol);Grid.SetRow(lstBox, iRow);
lstBox.Width = 300;

lstBox.Height = 100;


CreateListBoxItem(lstBox, "Asia");
CreateListBoxItem(lstBox, "Australia");CreateListBoxItem(lstBox,
"Antarctica");
myGrid.Children.Add(lstBox);

}
private void CreateListBoxItem(ListBox lstBox, String sItem)
{
ListBoxItem lstBoxItem1 = new ListBoxItem();
lstBoxItem1.Content = sItem;

lstBox.Items.Add(lstBoxItem1);

}




private void CreateComboBox(int iRow, int iCol)
{
myGrid.RowDefinitions.Add(new RowDefinition());
myGrid.ColumnDefinitions.Add(new ColumnDefinition());
ComboBox cmbBox = new ComboBox();
Grid.SetColumn(cmbBox, iCol);
Grid.SetRow(cmbBox, iRow);
cmbBox.Width = 100;

cmbBox.Height = 40;


CreateComboBoxItem(cmbBox, "Apples");
CreateComboBoxItem(cmbBox, "Banana");cmbBox.Name =
"ComboBox_" + iRow;
myGrid.Children.Add(cmbBox);

}
private void CreateComboBoxItem(ComboBox cmbBox,String sItem)
{
ComboBoxItem cmbBoxItem1 = new ComboBoxItem();
cmbBoxItem1.Content = sItem;

cmbBox.Items.Add(cmbBoxItem1);

}




private void CreateCheckBox(int iRow, int iCol)
{
myGrid.RowDefinitions.Add(new RowDefinition());
myGrid.ColumnDefinitions.Add(new ColumnDefinition());
CheckBox chkBox = new CheckBox();
Grid.SetColumn(chkBox, iCol);
Grid.SetRow(chkBox, iRow);
chkBox.Width = 100;

chkBox.Height = 40;
chkBox.Name = "CheckBox_" + iRow;chkBox.Content = "item 1";


myGrid.Children.Add(chkBox);



}
private void CreateRadioButton(int iRow, int iCol)
{
myGrid.RowDefinitions.Add(new RowDefinition());
myGrid.ColumnDefinitions.Add(new ColumnDefinition());
RadioButton lstBox = new RadioButton();
Grid.SetColumn(lstBox, iCol);
Grid.SetRow(lstBox, iRow);
lstBox.Width = 100;

lstBox.Height = 40;
lstBox.Content = "item 1";lstBox.GroupName = "A";


myGrid.Children.Add(lstBox);





}







}

}

How to upload app to macOS

1. Open Terminal Press Cmd (⌘) + Space , type Terminal , and hit Enter . 2. Navigate to Your Build Output Directory Your .app file is likel...