Showing posts with label DataBinding. Show all posts
Showing posts with label DataBinding. Show all posts

Sunday, April 13, 2008

Navigating Through Bound Data

About a month ago I had a requirement for a personal application I was building:
1. Show one piece of data through a label.
2. Have a Next and Previous button.

Like this:
Pretty simple window. Very hard to figure out. It took A LOT of study. Frankly I was surprised I couldn't find a clean cut answer anywhere for how to do this...hence, the following post.

The basic steps:
1. Add a class variable to store the CollectionView (tracks current record being displayed).
2. Set this.DataContext to your data source.
3. Set the variable you created in step one to (CollectionView)
CollectionViewSource.GetDefaultView(DataContext)

4. Add event handling for your variable's CollectionView.CurrentChanged event.
Here's the XAML:
   1:  <Window x:Class="DataBindingExample.DataNavigatorWithDataSet"
   2:      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:      Title="DataNavigatorWithDataSet" Height="190" Width="460">
   5:      <Grid>
   6:          <Button HorizontalAlignment="Right" Margin="0,55,24,67" Width="67" Content="&gt;&gt;" x:Name="btnNext" Click="btnNext_Click" />
   7:          <Button HorizontalAlignment="Left" Margin="8,55,0,67" Width="69" Content="&lt;&lt;" x:Name="btnPrevious" Click="btnPrevious_Click" />
   8:          <Label Content="{Binding Path=FirstName}" Margin="147.407,50,160.747,67" Name="label1" BorderBrush="Bisque" BorderThickness="1"></Label>
   9:      </Grid>
  10:  </Window>
Here's the code behind:
   1:  using System.Windows;
   2:  using System.Windows.Data;
   3:   
   4:  namespace DataBindingExample
   5:  {
   6:      public partial class DataNavigatorWithDataSet : Window
   7:      {
   8:          private CollectionView view;
   9:   
  10:          public DataNavigatorWithDataSet()
  11:          {
  12:              InitializeComponent();
  13:              PopulatePeople();
  14:          }
  15:   
  16:          private void PopulatePeople()
  17:          {
  18:              Data data = new Data();
  19:              DataContext = data.GetPeopleDataSet().PersonDataTable;
  20:              view = (CollectionView)CollectionViewSource.GetDefaultView(DataContext);
  21:   
  22:              view.CurrentChanged += delegate
  23:                                         {
  24:                                             btnPrevious.IsEnabled = (view.CurrentPosition > 0);
  25:                                             btnNext.IsEnabled = (view.CurrentPosition < view.Count - 1);
  26:                                         };
  27:          }
  28:   
  29:          private void btnNext_Click(object sender, RoutedEventArgs e)
  30:          {
  31:              view.MoveCurrentToNext();
  32:          }
  33:   
  34:          private void btnPrevious_Click(object sender, RoutedEventArgs e)
  35:          {
  36:              view.MoveCurrentToPrevious();
  37:          }
  38:      }
  39:  }

Tuesday, April 8, 2008

Element to Element Data Binding

<Slider Name="sliderFontSize" Minimum="1" Maximum="40" Value="10" TickFrequency="1" TickPlacement="TopLeft" />
<TextBlock Name="lblSampleText" Text="Simple Text" FontSize="{Binding Source=sliderFontSize, Path=Value}" />
The TextBlock's FontSize will change to the slider control's value. This binding in the xaml is saying, "Create a binding to the sliderFontSize.Value property."
Here's how you would create the equivalent binding in C#:
Binding binding = new Binding();
binding.Source = sliderFontSize;
binding.Path = new PropertyPath("Value");
lblSampleText.SetBinding(TextBlock.FontSizeProperty, binding);
More Info: Data Binding