Contains classes used for binding properties to data sources
Search This Blog
Thursday, June 12, 2014
Tuesday, June 3, 2014
ComboBoxItem WPF Example
WPF > Controls > ContentControl > ComboBoxItem
ComboBoxItem represent an item in a ComboBox.
Example:
XAML
ComboBoxItem represent an item in a ComboBox.
Example:
XAML
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox x:Name="cbo"
HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" SelectionChanged="ComboBox_SelectionChanged">
<ComboBoxItem>Option1</ComboBoxItem>
<ComboBoxItem>Option2</ComboBoxItem>
</ComboBox>
</Grid>
</Window>
C#
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string value = ((ComboBoxItem)cbo.SelectedItem).Content.ToString();
}
Labels:
ContentControl
WPF Read RSS feed and bind to DataGrid
WPF > SyndicationFeed > Read RSS feed
Read RSS feed and bind to DataGrid
XAML
Read RSS feed and bind to DataGrid
XAML
<Window x:Class="Feed.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Rss
Feed" Height="670" Width="737" Loaded="Window_Loaded">
<Grid>
<DataGrid ItemsSource="{Binding RssFeedList}" AutoGenerateColumns="False"
IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Title" Binding="{Binding Title}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
c#
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
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 Feed
{
public class RssFeed
{
public string Title { get; set; }
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public ObservableCollection<RssFeed> RssFeedList { get; set; }
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
string feedUrl = "http://rss.cnn.com/rss/cnn_latest.rss";
System.Xml.XmlReader reader =
System.Xml.XmlReader.Create(feedUrl);
System.ServiceModel.Syndication.SyndicationFeed feed =
System.ServiceModel.Syndication.SyndicationFeed.Load(reader);
RssFeedList = new ObservableCollection<RssFeed>();
foreach (var item in feed.Items)
{
RssFeed rf = new RssFeed();
rf.Title =
item.Title.Text.ToString();
RssFeedList.Add(rf);
}
this.DataContext = this;
}
}
}
Labels:
SyndicationFeed
Wednesday, May 21, 2014
Get tree visual elements WPF
VisualTreeHelper provides methods that perform
tasks involving nodes in a visual tree.
Example:
Get tree visual elements for user control
FrameworkElement fe = (FrameworkElement)YourControl;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(fe); i++)
{
Grid childVisual = (Grid)VisualTreeHelper.GetChild(fe, i);
childVisual.Width = sp.ActualWidth;
}
Labels:
WPF
Monday, May 19, 2014
WPF Converter Example
WPF > Data > Converters
Converters changes data from one type to another and apply custom logic to binding.
Implementation : create a class that implements
the IvalueConverter.
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:local="clr-namespace:WpfApplication2"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:YesNoConverter x:Key="YesNoConverter" />
</Window.Resources>
<StackPanel Margin="5">
<CheckBox x:Name="chk" IsChecked="{Binding ElementName=txtYesNo, Path=Text, Converter={StaticResource
YesNoConverter}}" Content="Yes/No"
/>
<TextBox Name="txtYesNo" />
</StackPanel>
</Window>
C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public class YesNoConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch (value.ToString())
{
case "Yes":
return true;
case "No":
return false;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is bool)
{
if ((bool)value == true)
return "Yes";
else
return "No";
}
return "No";
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
Labels:
Binding
Binding path to current source WPF
WPF > Binding > Path
What is Binding Path=. ?
Answer:
Labels:
Binding
Tuesday, April 8, 2014
Expander Control WPF Example
WPF > Controls > Layout > Expander
Expander displays a header that has a collapsible window that displays content when user clicks arrow button.
Example
XAML
Expander displays a header that has a collapsible window that displays content when user clicks arrow button.
Example
XAML
<Window x:Class="ImageManager.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<Expander Header="Expander1"
HorizontalAlignment="Left" Margin="59,94,0,0" VerticalAlignment="Top">
<TextBlock Text="Expander 1" />
</Expander>
<Expander Header="Expander2"
HorizontalAlignment="Left" Margin="59,141,0,0" VerticalAlignment="Top">
<TextBlock Text="Expander 2" />
</Expander>
</Grid>
</Window>
Labels:
Layout
Subscribe to:
Posts (Atom)