tags:

views:

44

answers:

2

Hi , I have a doubt binding a textbox.he scenario is like this.I hava a dataset say,

DataTable dt=new DataTable();
dt.TableName = "table";
dt.Columns.Add("mode", typeof(int));
dt.Columns.Add("value", typeof(int));
DataRow dr = dt.NewRow();
dr["mode"] = 1;
dr["value"] = 1000;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["mode"] = 2;
dr["value"] = 2000;
dt.Rows.Add(dr);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
this.DataContext = ds;

The window is bound to this dataset.I have textbox in my window and i want to bind it to the row with mode=1, so that i can show that rows value in the text property of my textbox.

How can i apply this binding..?

Any input will be highly helpfull

+1  A: 

DataSets are a bit generic to be using for binding in WPF. Its usually easier to use the M-V-VM pattern where you have models that are INotifyPropertyChanged or DependencyObjects that your UI binds against.

I'm not sure if you're talking about changing what things are bound to depending on the "mode" or if you just want to filter on "mode."

In the first case, you'd have to use a DataTrigger on a Style in order to change the ContentTemplate that you're using based on the value of your mode field. This is NOT an easy concept for the beginner or intermediate user.

This is a decent blog post with instructions on how to accomplish this. Again, its pretty confusing and when it doesn't work its sometimes hard to troubleshoot.

In the second case, you'd be better served by setting your DataContext to a type that contains multiple DataTables that are pre-filtered. Filtering isn't a job for the UI, its a job for code. It might look something like:

public class MyDataContext
{
  public DataTable ModeOne {get;set;}
  public DataTable ModeTwo {get;set;}
}

or perhaps

public class MyDataContext
{
  public Dictionary<int, DataTable> TableByMode {get;set;}
}

where you would bind like this

<ItemsControl Content="{Binding TableByMode[1]}">
Will
A: 

Thanks will.I was looking for the second case.Any way thanks for the lead.