What is the difference between a Window and Page item in a WPF project?
Windows are independent and normal WPF application windows like Forms in WinForm Application.
A window can contain WPF Pages using Frame Container.
Pages on the other hand are intended for use in Navigation applications, where we can navigate between pages from within a Window. Examples of Pages are in Wizard kind of applications where each screen in a wizard can be a page, which are hosted within the main wizard window. Pages are hosted in NavigationWindows or Frames.
Tuesday, 24 September 2013
WPF - How to Read each line from a TextBox
To read each line from a TextBox control in WPF, we can use the below code snippet.
StringCollection type represents a collection of strings. The type is part of the namespace - System.Collections.Specialized. import the namespace in your code file:
The usage is straightforward as below:
Please note that each item of the StringCollection is a string holding each line of the TextBox.
StringCollection type represents a collection of strings. The type is part of the namespace - System.Collections.Specialized. import the namespace in your code file:
using System.Collections.Specialized;
private StringCollection GetTextLines() { var lines = new StringCollection(); int lineCount = txtPlainTxt.LineCount; for (int line = 0; line < lineCount; line++) lines.Add(txtPlainTxt.GetLineText(line)); return lines; }
The usage is straightforward as below:
Please note that each item of the StringCollection is a string holding each line of the TextBox.
// Usage var lines = GetTextLines(); foreach (var line in lines) { // line is string with each line. }
WPF - How to set Start Window
To set the Start window / page in a WPF app, we can configure it in App.xaml file.
In the "Application" element of App.xaml file in your project, set the "StartupUri" attribute to the xaml page you want to load first in the WPF Application.
In the below sample code "QuizTxt2XmlPage.xaml" is the start up page.
A reference screen shot is provided below:
In the "Application" element of App.xaml file in your project, set the "StartupUri" attribute to the xaml page you want to load first in the WPF Application.
In the below sample code "QuizTxt2XmlPage.xaml" is the start up page.
<Application x:Class="Quiz.WpfApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="QuizTxt2XmlPage.xaml">
A reference screen shot is provided below:
Saturday, 21 September 2013
SQL Server:Login failed for user 'sa'. (Microsoft SQL Server, Error: 18456)
Sometimes you can connect to MS SQL Server Management Studio with Windows Authentication but is unable to connect with SQL Server Authentication. You might get the below error message:
The most probable reason for this could be that when you first installed SQL Server and configured, you might have set the Server Authentication mode to Windows Authentication Mode only. Follow the below steps to solve this issue.
Login failed for user 'sa'. (Microsoft SQL Server, Error: 18456) |
- Login to the MS SQL Server Management Studio with Windows Authentication.
- In SQL Server Management Studio Object Explorer, right-click the server, and then click Properties.
- In the Server Properties window, select "Security" under "Select a page" section.
- Select the Server authentication as "SQL Server and Windows Authentication mode" and click Ok.
- Restart the SQL Services and then try to login with 'sa' details.
Set Server Authentication mode to SQL Server and Windows |
Restart MSSQL servies before trying to login again. |
Thursday, 5 September 2013
Windows Phone - How to use Resource Files
Resource Files in Windows phone has the similar concepts as it was in Web Forms. It can be used for localization as well as store string values to be used across your apps. Resource files are a nice way to reduce hardcoding strings inside your application code.
The default resource file added to your solution when the project is created is "AppResources.resx" which is in the "Resources" folder at the root of your project folder hierarchy. This default file is called the "neutral language resource" because it is the fallback resource file even in localization scenarios.
Note: As in web forms and win forms, each culture has its own resource file, with the culture suffixed to the filename (AppResources.en-US.resx). We will discuss more on localization scenarios in another post.
Below is the code to dynamically bind your resource to XAML page element's properties.
The default resource file added to your solution when the project is created is "AppResources.resx" which is in the "Resources" folder at the root of your project folder hierarchy. This default file is called the "neutral language resource" because it is the fallback resource file even in localization scenarios.
Note: As in web forms and win forms, each culture has its own resource file, with the culture suffixed to the filename (AppResources.en-US.resx). We will discuss more on localization scenarios in another post.
Below is the code to dynamically bind your resource to XAML page element's properties.
"{Binding Path=LocalizedResources.ApplicationHeader, Source={StaticResource LocalizedStrings}}"For example if we want to bind a string resource to the Text property of TextBlock Control:
<TextBlock Text="{Binding Path=LocalizedResources.ApplicationHeader, Source={StaticResource LocalizedStrings}}" />
Wednesday, 4 September 2013
Windows Phone - How to include ampersand (&) in the content of XAML elements like Combobox and TextBlock
/* XAML */
Entity references or sequences beginning with an ampersand '&' must be terminated with a semicolon ';'.
The solution is to encode the special characters:
Use & to encode the ampersand.
/* XAML */
Alternative:
/* XAML */
XAML being xml based, the xml parser when parsing an xml element, it also parses the text between XML tags, because XML elements can contain child elements.
CDATA indicated the text that should not be parsed by the XML parser. So any text that includes characters like '<' and '&' are to be included in CDATA, because these characters have special meaning in XML.
CDATA section starts with "<![CDATA[" and ends with "]]>"
<ComboBox><ComboBoxItem> Awake & Alive</ComboBoxItem></ComboBox>The above code throws exception :
Entity references or sequences beginning with an ampersand '&' must be terminated with a semicolon ';'.
The solution is to encode the special characters:
Use & to encode the ampersand.
/* XAML */
<ComboBox><ComboBoxItem> Awake & Alive</ComboBoxItem></ComboBox>The same is applicable for other elements like TextBlock
Alternative:
/* XAML */
<ComboBox><ComboBoxItem><![CDATA[Awake & Alive]]></ComboBoxItem></ComboBox>Explanation:
XAML being xml based, the xml parser when parsing an xml element, it also parses the text between XML tags, because XML elements can contain child elements.
CDATA indicated the text that should not be parsed by the XML parser. So any text that includes characters like '<' and '&' are to be included in CDATA, because these characters have special meaning in XML.
CDATA section starts with "<![CDATA[" and ends with "]]>"
Subscribe to:
Posts (Atom)