WPF Windows Namespace contains base classes, classes that support the WPF property system and event logic.
Search This Blog
Tuesday, November 19, 2013
WPF Windows Namespace
WPF > Windows
WPF Windows Namespace contains base classes, classes that support the WPF property system and event logic.
WPF Windows Namespace contains base classes, classes that support the WPF property system and event logic.
Labels:
Windows
WPF DependencyProperty Example
WPF > Windows > DependencyProperty
DependencyProperty is a property that can be set through methods such as, styling, data binding, animation, and inheritance.
Example: Create a numeric TextBox with DependencyProperty
Create new WPF User Control Library
NumericTextBox.xaml
<TextBox x:Class="NumericTextBox.NumericTextBox"
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"
>
</TextBox>
NumericTextBox.xaml.cs
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace NumericTextBox
{
/// <summary>
/// Interaction logic for NumericTextBox.xaml
/// </summary>
public partial class NumericTextBox : TextBox
{
public NumericTextBox()
{
InitializeComponent();
this.PreviewTextInput += OnPreviewTextInput;
}
/// <summary>
/// The max value property
/// </summary>
typeof(NumericTextBox), new UIPropertyMetadata(null));
get { return (double?)GetValue(MaxValueProperty); }
set { SetValue(MaxValueProperty, value); }
}
/// </summary>
typeof(NumericTextBox), new UIPropertyMetadata(null));
get { return (double?)GetValue(MinValueProperty); }
set { SetValue(MinValueProperty, value); }
}
var regex = new Regex("[^0-9.]+");
return !regex.IsMatch(text);
}
private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (this.CheckNumeric(e.Text))
{
if (e.Text == "." && this.Text.Contains("."))
e.Handled = true;
else
{
// Check min value
if (MinValue != null && Convert.ToDouble(this.Text + e.Text) < MinValue)
{
this.Text = MinValue.ToString();
this.SelectionStart = this.Text.Length;
e.Handled = true;
}
// Check max value
if (MaxValue != null && Convert.ToDouble(this.Text + e.Text) > MaxValue)
{
this.Text = MaxValue.ToString();
this.SelectionStart = this.Text.Length;
e.Handled = true;
}
}
}
else
e.Handled = true;
}
}
}
DependencyProperty is a property that can be set through methods such as, styling, data binding, animation, and inheritance.
Example: Create a numeric TextBox with DependencyProperty
NumericTextBox.xaml
<TextBox x:Class="NumericTextBox.NumericTextBox"
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"
>
</TextBox>
NumericTextBox.xaml.cs
using System;
using System.Collections.Generic;using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace NumericTextBox
{
/// <summary>
/// Interaction logic for NumericTextBox.xaml
/// </summary>
public partial class NumericTextBox : TextBox
{
public NumericTextBox()
{
InitializeComponent();
this.PreviewTextInput += OnPreviewTextInput;
}
/// The max value property
/// </summary>
public static readonly DependencyProperty MaxValueProperty =
DependencyProperty.Register("MaxValue", typeof(double?),typeof(NumericTextBox), new UIPropertyMetadata(null));
public double? MaxValue
{get { return (double?)GetValue(MaxValueProperty); }
set { SetValue(MaxValueProperty, value); }
}
/// <summary>
/// The min value property/// </summary>
public static readonly DependencyProperty MinValueProperty =
DependencyProperty.Register("MinValue", typeof(double?),typeof(NumericTextBox), new UIPropertyMetadata(null));
public double? MinValue
{get { return (double?)GetValue(MinValueProperty); }
set { SetValue(MinValueProperty, value); }
}
private bool CheckNumeric(string text)
{var regex = new Regex("[^0-9.]+");
return !regex.IsMatch(text);
}
private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (this.CheckNumeric(e.Text))
{
if (e.Text == "." && this.Text.Contains("."))
e.Handled = true;
else
{
// Check min value
if (MinValue != null && Convert.ToDouble(this.Text + e.Text) < MinValue)
{
this.Text = MinValue.ToString();
this.SelectionStart = this.Text.Length;
e.Handled = true;
}
// Check max value
if (MaxValue != null && Convert.ToDouble(this.Text + e.Text) > MaxValue)
{
this.Text = MaxValue.ToString();
this.SelectionStart = this.Text.Length;
e.Handled = true;
}
}
}
else
e.Handled = true;
}
}
}
MainWindow.xaml
You can set MinValue and MaxValue properties
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:NumericTextBox="clr-namespace:NumericTextBox;assembly=NumericTextBox" x:Class="WpfApplication3.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Grid>
<NumericTextBox:NumericTextBox
HorizontalAlignment="Left" Margin="98,89,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top"
Width="201" MinValue="1" MaxValue="100000"/>
</Grid>
</Window>
Labels:
DependencyProperty,
Windows
WPF Frame Control
WPF > Controls > ContentControl > Frame
WPF frame control is capable of displaying both WPF and HTML content. Also it allows navigation between different contents.
Example:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
xmlns:src="clr-namespace:WpfApplication2" Width="362.5"
>
<StackPanel>
<Frame Source="Page1.xaml">
</Frame>
<Frame Source="Page1.xaml">
<Frame.LayoutTransform>
<RotateTransform Angle="90" />
</Frame.LayoutTransform>
</Frame>
</StackPanel>
</Window>
WPF frame control is capable of displaying both WPF and HTML content. Also it allows navigation between different contents.
Example:
XAML:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
xmlns:src="clr-namespace:WpfApplication2" Width="362.5"
>
<StackPanel>
<Frame Source="Page1.xaml">
</Frame>
<Frame Source="Page1.xaml">
<Frame.LayoutTransform>
<RotateTransform Angle="90" />
</Frame.LayoutTransform>
</Frame>
</StackPanel>
</Window>
Labels:
ContentControl
WPF Binding to Web Service Example
WPF > Binding > Web Service
Add reference to your Web service. We will use com.microsoft.msdn.services as example:
XAML:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="300"
xmlns:src="clr-namespace:WpfApplication2"
>
<Grid>
<ListView ItemsSource="{Binding Path=availableVersionsAndLocales}" HorizontalAlignment="Left" Height="240" VerticalAlignment="Top" Width="266">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=locale}" Header="Locale" />
<GridViewColumn DisplayMemberBinding="{Binding Path=version}" Header="Version" />
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
C# Code:
using WpfApplication2.com.microsoft.msdn.services;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
getContentRequest request = new getContentRequest();
request.contentIdentifier = "abhtw0f1";
ContentService proxy = new ContentService();
this.DataContext = proxy.GetContent(request);
}
}
}
Result:
Add reference to your Web service. We will use com.microsoft.msdn.services as example:
XAML:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="300"
xmlns:src="clr-namespace:WpfApplication2"
>
<Grid>
<ListView ItemsSource="{Binding Path=availableVersionsAndLocales}" HorizontalAlignment="Left" Height="240" VerticalAlignment="Top" Width="266">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=locale}" Header="Locale" />
<GridViewColumn DisplayMemberBinding="{Binding Path=version}" Header="Version" />
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
C# Code:
using WpfApplication2.com.microsoft.msdn.services;
namespace WpfApplication2
{/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
getContentRequest request = new getContentRequest();
request.contentIdentifier = "abhtw0f1";
ContentService proxy = new ContentService();
this.DataContext = proxy.GetContent(request);
}
}
}
Result:
Labels:
Binding
Monday, November 18, 2013
Filter DataGrid WPF C# Example
WPF > Controls > ItemControls > DataGrid > Filter
Using ICollectionView to filter datagrid.
XAML:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Title="MainWindow" Height="447.91" Width="514.871" Loaded="Window_Loaded">
<Grid Margin="0,0,2,81">
<DataGrid x:Name="dg" HorizontalAlignment="Left" VerticalAlignment="Top" Height="413" Width="507" AutoGenerateColumns="False" Margin="0,0,-2,-76">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}"
Header="Id">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Content, RelativeSource=
{RelativeSource Mode=TemplatedParent}}"/>
<TextBox x:Name="txtId" Width="100" TextChanged="txtName_TextChanged" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Name}"
Header="Name">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Content, RelativeSource=
{RelativeSource Mode=TemplatedParent}}"/>
<TextBox x:Name="txtName" Width="100" TextChanged="txtName_TextChanged" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
C# Code
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public class Skill
{
public int Years { get; set; }
public string Name { get; set; }
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Position { get; set; }
public List<Skill> SkillList { get; set; }
}
public class Persons : ObservableCollection<Person>
{
public Persons()
{
Add(new Person() { Id = 1, Name = "Person1", Position = "Manager", SkillList = new List<Skill>() });
Add(new Person() { Id = 2, Name = "Programmer2", Position = "Programmer", SkillList = new List<Skill>() });
Add(new Person() { Id = 3, Name = "Person3", Position = "Programmer", SkillList = new List<Skill>() });
Add(new Person() { Id = 4, Name = "Person4", Position = "Admin", SkillList = new List<Skill>() });
Add(new Person() { Id = 5, Name = "Person5", Position = "Tester", SkillList = new List<Skill>() });
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Persons pList = new Persons();
dg.ItemsSource = pList;
}
private void txtName_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox t = (TextBox)sender;
string filter = t.Text;
ICollectionView cv = CollectionViewSource.GetDefaultView(dg.ItemsSource);
if (filter == "")
cv.Filter = null;
else
{
cv.Filter = o =>
{
Person p = o as Person;
if (t.Name == "txtId")
return (p.Id == Convert.ToInt32(filter));
return (p.Name.ToUpper().StartsWith(filter.ToUpper())) ;
};
}
}
}
}
Using ICollectionView to filter datagrid.
XAML:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Title="MainWindow" Height="447.91" Width="514.871" Loaded="Window_Loaded">
<Grid Margin="0,0,2,81">
<DataGrid x:Name="dg" HorizontalAlignment="Left" VerticalAlignment="Top" Height="413" Width="507" AutoGenerateColumns="False" Margin="0,0,-2,-76">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}"
Header="Id">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Content, RelativeSource=
{RelativeSource Mode=TemplatedParent}}"/>
<TextBox x:Name="txtId" Width="100" TextChanged="txtName_TextChanged" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Name}"
Header="Name">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Content, RelativeSource=
{RelativeSource Mode=TemplatedParent}}"/>
<TextBox x:Name="txtName" Width="100" TextChanged="txtName_TextChanged" />
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
namespace WpfApplication2
{/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public class Skill
{
public int Years { get; set; }
public string Name { get; set; }
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Position { get; set; }
public List<Skill> SkillList { get; set; }
}
public class Persons : ObservableCollection<Person>
{
public Persons()
{
Add(new Person() { Id = 1, Name = "Person1", Position = "Manager", SkillList = new List<Skill>() });
Add(new Person() { Id = 2, Name = "Programmer2", Position = "Programmer", SkillList = new List<Skill>() });
Add(new Person() { Id = 3, Name = "Person3", Position = "Programmer", SkillList = new List<Skill>() });
Add(new Person() { Id = 4, Name = "Person4", Position = "Admin", SkillList = new List<Skill>() });
Add(new Person() { Id = 5, Name = "Person5", Position = "Tester", SkillList = new List<Skill>() });
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Persons pList = new Persons();
dg.ItemsSource = pList;
}
private void txtName_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox t = (TextBox)sender;
string filter = t.Text;
ICollectionView cv = CollectionViewSource.GetDefaultView(dg.ItemsSource);
if (filter == "")
cv.Filter = null;
else
{
cv.Filter = o =>
{
Person p = o as Person;
if (t.Name == "txtId")
return (p.Id == Convert.ToInt32(filter));
return (p.Name.ToUpper().StartsWith(filter.ToUpper())) ;
};
}
}
}
}
Labels:
ItemsControl
Subscribe to:
Posts (Atom)