none
XAML & DataSet binding RRS feed

  • Question

  •  

    hi everybody,

    not sure if i'm posting in the right forum - first time poster here, but anyways...

     

    i threw up a quick sample here to specificaly show, what i want to do.

     

    i have this dataset:

     

    Code Snippet

    this.dataSet.Tables.Add("Master");

    this.dataSet.Tables.Add("Items");

     

    this.dataSet.Tables["Master"].Columns.Add("master_id");

    this.dataSet.Tables["Master"].Columns.Add("item_id");

     

    this.dataSet.Tables["Items"].Columns.Add("item_id");

    this.dataSet.Tables["Items"].Columns.Add("name");

     

    DataRow itemRow1 = this.dataSet.Tables["Items"].NewRow();

    itemRow1["item_id"] = 1;

    itemRow1["name"] = "one";

    this.dataSet.Tables["Items"].Rows.Add(itemRow1);

     

    DataRow itemRow2 = this.dataSet.Tables["Items"].NewRow();

    itemRow2["item_id"] = 2;

    itemRow2["name"] = "two";

    this.dataSet.Tables["Items"].Rows.Add(itemRow2);

     

    DataRow masterRow1 = this.dataSet.Tables["Master"].NewRow();

    masterRow1["master_id"] = 1;

    masterRow1["item_id"] = 1;

    this.dataSet.Tables["Master"].Rows.Add(masterRow1);

     

    DataRow masterRow2 = this.dataSet.Tables["Master"].NewRow();

    masterRow2["master_id"] = 2;

    masterRow2["item_id"] = 2;

    this.dataSet.Tables["Master"].Rows.Add(masterRow2);

     

    this.dataSet.Relations.Add("rel", this.dataSet.Tables["Items"].Columns["item_id"], this.dataSet.Tables["Master"].Columns["item_id"]);

    this.DataContext = this.dataSet;

     

     

    and i'm binding it in XAML like this:

     

    Code Snippet

    <StackPanel>

    <ListView ItemsSource="{Binding Master}" Height="124" VerticalAlignment="Top" IsSynchronizedWithCurrentItem="True">

    <ListView.View>

    <GridView>

    <GridViewColumn Header="Master ID" DisplayMemberBinding="{Binding master_id}" />

    <GridViewColumn Header="Master item ID" DisplayMemberBinding="{Binding item_id}" />

    <GridViewColumn Header="Item ID" DisplayMemberBinding="{Binding rel/item_id}" />

    <GridViewColumn Header="Item name" DisplayMemberBinding="{Binding rel/name}" />

    </GridView>

    </ListView.View>

    </ListView>

    <ComboBox ItemsSource="{Binding Items}" DisplayMemberPath="name" SelectedValuePath="item_id" SelectedValue="{Binding Master/item_id}"/>

    </StackPanel>

     

     

    now - the problem:

    the combobox below grid works ok, it changes after i select other master. if i replace the 3rd and 4th columns in grid with celltemplated comboboxes with itemssource etc. set, it works ok. but, what i want to do, is, leave plain textblocks, just show the item name from other table.

    Wednesday, February 27, 2008 11:07 AM

Answers

  • You can employ a converter to do the trick:

    Code Snippet
    <Window x:Class="AnswerHarness.RelationalDataBindingDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="RelationalDataBindingDemo">
    <
    StackPanel>
    <
    ListView ItemsSource="{Binding Master}" Height="124" VerticalAlignment="Top" IsSynchronizedWithCurrentItem="True">
    <
    ListView.View>
    <
    GridView>
    <
    GridViewColumn Header="Master ID" DisplayMemberBinding="{Binding master_id}" />
    <
    GridViewColumn Header="Master item ID" DisplayMemberBinding="{Binding item_id}" />
    <
    GridViewColumn Header="Item name" x:Name="nameColumn"/>
    </
    GridView>
    </
    ListView.View>
    </
    ListView>
    <
    ComboBox
    ItemsSource="{Binding Items}"
    DisplayMemberPath="name"
    SelectedValuePath="item_id"
    SelectedValue="{Binding Master/item_id}"/>
    </
    StackPanel>
    </
    Window>
    public class DataConverter : IValueConverter
    {
    private DataSet dataSet = null;
    public DataConverter(DataSet dataSet)
    {
    this.dataSet = dataSet;
    }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    Object result = null;
    foreach (DataRow row in dataSet.Tables["Items"].Rows)
    {
    if (row["item_id"].ToString() == value.ToString())
    {
    result = row["name"];
    }

    }
    return result;
    }

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

    public partial class RelationalDataBindingDemo : Window
    {
    private DataSet dataSet = new DataSet();

    public RelationalDataBindingDemo()
    {
    InitializeComponent();
    this.dataSet.Tables.Add("Master");
    this.dataSet.Tables.Add("Items");
    this.dataSet.Tables["Master"].Columns.Add("master_id");
    this.dataSet.Tables["Master"].Columns.Add("item_id");
    this.dataSet.Tables["Items"].Columns.Add("item_id");
    this.dataSet.Tables["Items"].Columns.Add("name");
    DataRow itemRow1 = this.dataSet.Tables["Items"].NewRow();
    itemRow1["item_id"] = 1;
    itemRow1["name"] = "one";
    this.dataSet.Tables["Items"].Rows.Add(itemRow1);
    DataRow itemRow2 = this.dataSet.Tables["Items"].NewRow();
    itemRow2["item_id"] = 2;
    itemRow2["name"] = "two";
    this.dataSet.Tables["Items"].Rows.Add(itemRow2);
    DataRow masterRow1 = this.dataSet.Tables["Master"].NewRow();
    masterRow1["master_id"] = 1;
    masterRow1["item_id"] = 1;
    this.dataSet.Tables["Master"].Rows.Add(masterRow1);
    DataRow masterRow2 = this.dataSet.Tables["Master"].NewRow();
    masterRow2["master_id"] = 2;
    masterRow2["item_id"] = 2;
    this.dataSet.Tables["Master"].Rows.Add(masterRow2);
    this.dataSet.Relations.Add("rel", this.dataSet.Tables["Items"].Columns["item_id"], this.dataSet.Tables["Master"].Columns["item_id"]);
    this.DataContext = this.dataSet;
    Binding binding = new Binding("item_id");
    binding.Converter = new DataConverter(dataSet);
    this.nameColumn.DisplayMemberBinding = binding;
    }
    }

    Hope this helps
    Friday, February 29, 2008 4:44 AM

All replies

  • just BUMP

    Thursday, February 28, 2008 8:10 AM
  • You can employ a converter to do the trick:

    Code Snippet
    <Window x:Class="AnswerHarness.RelationalDataBindingDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="RelationalDataBindingDemo">
    <
    StackPanel>
    <
    ListView ItemsSource="{Binding Master}" Height="124" VerticalAlignment="Top" IsSynchronizedWithCurrentItem="True">
    <
    ListView.View>
    <
    GridView>
    <
    GridViewColumn Header="Master ID" DisplayMemberBinding="{Binding master_id}" />
    <
    GridViewColumn Header="Master item ID" DisplayMemberBinding="{Binding item_id}" />
    <
    GridViewColumn Header="Item name" x:Name="nameColumn"/>
    </
    GridView>
    </
    ListView.View>
    </
    ListView>
    <
    ComboBox
    ItemsSource="{Binding Items}"
    DisplayMemberPath="name"
    SelectedValuePath="item_id"
    SelectedValue="{Binding Master/item_id}"/>
    </
    StackPanel>
    </
    Window>
    public class DataConverter : IValueConverter
    {
    private DataSet dataSet = null;
    public DataConverter(DataSet dataSet)
    {
    this.dataSet = dataSet;
    }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    Object result = null;
    foreach (DataRow row in dataSet.Tables["Items"].Rows)
    {
    if (row["item_id"].ToString() == value.ToString())
    {
    result = row["name"];
    }

    }
    return result;
    }

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

    public partial class RelationalDataBindingDemo : Window
    {
    private DataSet dataSet = new DataSet();

    public RelationalDataBindingDemo()
    {
    InitializeComponent();
    this.dataSet.Tables.Add("Master");
    this.dataSet.Tables.Add("Items");
    this.dataSet.Tables["Master"].Columns.Add("master_id");
    this.dataSet.Tables["Master"].Columns.Add("item_id");
    this.dataSet.Tables["Items"].Columns.Add("item_id");
    this.dataSet.Tables["Items"].Columns.Add("name");
    DataRow itemRow1 = this.dataSet.Tables["Items"].NewRow();
    itemRow1["item_id"] = 1;
    itemRow1["name"] = "one";
    this.dataSet.Tables["Items"].Rows.Add(itemRow1);
    DataRow itemRow2 = this.dataSet.Tables["Items"].NewRow();
    itemRow2["item_id"] = 2;
    itemRow2["name"] = "two";
    this.dataSet.Tables["Items"].Rows.Add(itemRow2);
    DataRow masterRow1 = this.dataSet.Tables["Master"].NewRow();
    masterRow1["master_id"] = 1;
    masterRow1["item_id"] = 1;
    this.dataSet.Tables["Master"].Rows.Add(masterRow1);
    DataRow masterRow2 = this.dataSet.Tables["Master"].NewRow();
    masterRow2["master_id"] = 2;
    masterRow2["item_id"] = 2;
    this.dataSet.Tables["Master"].Rows.Add(masterRow2);
    this.dataSet.Relations.Add("rel", this.dataSet.Tables["Items"].Columns["item_id"], this.dataSet.Tables["Master"].Columns["item_id"]);
    this.DataContext = this.dataSet;
    Binding binding = new Binding("item_id");
    binding.Converter = new DataConverter(dataSet);
    this.nameColumn.DisplayMemberBinding = binding;
    }
    }

    Hope this helps
    Friday, February 29, 2008 4:44 AM