WPF Data Binding Using Code Behind and XAML

2011-10-05


You can use either of code behind or XAML to implement data binding in WPF:

1: Code behind binding:

There are 2 types of bindingL MultiBinding and Binding (single):

//binding background color
MultiBinding multiBinding = new MultiBinding();

multiBinding.Converter = new ViewModels.DeviceIOViewModel.TypeAndStatusToColorConverter();

multiBinding.Bindings.Add(new Binding
{
    Source = DeviceIODataList.DeviceList[this.DeviceIndex],  //this.IODataSource,
    Path = new PropertyPath("DeviceType")
});
multiBinding.Bindings.Add(new Binding
{
    Source = DeviceIODataList.DeviceList[this.DeviceIndex],
    Path = new PropertyPath("AIs[0].SensorStatus")
});

multiBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
multiBinding.Mode = BindingMode.OneWay;

//first clear binding
BindingOperations.ClearBinding(UBorder, Border.BackgroundProperty);
//BindingOperations.SetBinding(this.textBox1, TextBox.BackgroundProperty, bind);
this.UBorder.SetBinding(Border.BackgroundProperty, multiBinding);

///////////////////////
//binding device type

Binding bind = new Binding();

bind.Source = DeviceIODataList.DeviceList[this.DeviceIndex];
bind.Path = new PropertyPath("DeviceType");
bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
bind.Mode = BindingMode.OneWay;

this.lblDeviceType.SetBinding(Label.ContentProperty, bind);

The XAML code:

<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
     <Grid.RowDefinitions>
         <RowDefinition Height="28*" />
         <RowDefinition Height="auto"/>
         <RowDefinition Height="23*" />
     </Grid.RowDefinitions>
     <Label Grid.Row="0"  Name="lblDeviceType" VerticalAlignment="Center" HorizontalAlignment="Center" Content="Device Type" FontSize="14" />
     <Label Grid.Row="1"  Name="lblValue" VerticalAlignment="Center" HorizontalAlignment="Center" Content="Value" FontSize="16" FontWeight="Bold" />
     <Label Grid.Row="2"  Name="lblStatus" VerticalAlignment="Center" HorizontalAlignment="Center" Content="Status" />
 </Grid >

2: XAML binding:

Now we use another sample which is from here:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Orientation="Vertical">
        <StackPanel.Resources>
            <local:FoodIndexConverter x:Key="foodIndexConverter" />
        </StackPanel.Resources>
        <TextBlock Text="{Binding DessertIndex}" />
        <TextBlock Text="{Binding Food[2]}" />
        <TextBlock>
                <TextBlock.Text>
                    <MultiBinding Converter="{StaticResource foodIndexConverter}">
                        <Binding Path="DessertIndex" />
                        <Binding Path="Food"/>
                    </MultiBinding>
                </TextBlock.Text>
        </TextBlock>
    </StackPanel>
</Window>
namespace WpfApplication1
{
    public class FoodIndexConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values == null || values.Length != 2)
                return null;

            int? idx = values[0] as int?;
            object[] food = values[1] as object[];

            if (!idx.HasValue || food == null)
                return null;

            return food[idx.Value];
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}