Can't get two-way binding to work :) - wpf

I have a project which sets up dynamic tabs for monitoring and thus hosts several "identity" objects which are INotifyPropertyChanged and work individually as tested in a dummy app. However, I can't get the bindings to work correctly in the app I'm working on. They are set up i.e.:
(Configuration.Identities is an ObservableCollection<Identity>)
foreach (Identity id in Configuration.Identities)
{
workTab = new TabItem();
workSettingsTab = new SettingsTabItem();
workTab.DataContext = id;
Binding workTabHeaderBinding = new Binding("Username");
workTabHeaderBinding.Converter = new ToLowerConverter();
workTab.SetBinding(TabItem.HeaderProperty, workTabHeaderBinding);
workSettingsTab.DataContext = id;
Binding workSettingsTabHeaderBinding = new Binding("Username");
workSettingsTabHeaderBinding.Converter = new ToUpperConverter();
workSettingsTab.SetBinding(TabItem.HeaderProperty, workSettingsTabHeaderBinding);
This stuff above works fine, presumably because it's one-way. But these two only work one-way:
workSettingsTab.txtUsername.DataContext = id;
Binding usernameBinding = new Binding("Username");
usernameBinding.Mode = BindingMode.TwoWay;
workSettingsTab.txtUsername.SetBinding(TextBox.TextProperty, usernameBinding);
Binding getJournals = new Binding("LoadJournals");
scrapeJournals.Mode = BindingMode.TwoWay;
workSettingsTab.chkScrapeJournals.DataContext = id;
workSettingsTab.chkScrapeJournals.SetBinding(CheckBox.IsCheckedProperty, scrapeJournals);
I noticed when debugging my save-routine, that the Identity object referenced in DataContext in above controls does indeed contain the changed values, but the one in the ObservableCollection I used in the foreach above is not updated. So somehow the Identity object gets copied from the one initially used to set the DataContext property during the foreach. What am I doing wrong here?

Right. I can't get this to make sense, but so far:
ObservableCollection<SwitchClass> col2 = new ObservableCollection<SwitchClass>();
col2.Add(new SwitchClass{theSwitch=false});
col2.Add(new SwitchClass{theSwitch=true});
col2.Add(new SwitchClass{theSwitch=false});
col2.Add(new SwitchClass{theSwitch=true});
customTab cTab;
Int32 z = 0;
Binding b;
foreach (SwitchClass s in col2)
{
cTab = new customTab();
cTab.theCheckbox.DataContext = s;
b = new Binding("theSwitch");
b.Mode = BindingMode.TwoWay;
cTab.SetBinding(CheckBox.IsCheckedProperty, b);
cTab.Header = "Tab " + z.ToString();
z++;
tabControl.Items.Add(cTab);
}
Does not work, but:
ObservableCollection<SwitchClass> col2 = new ObservableCollection<SwitchClass>();
col2.Add(new SwitchClass{theSwitch=false});
col2.Add(new SwitchClass{theSwitch=true});
col2.Add(new SwitchClass{theSwitch=false});
col2.Add(new SwitchClass{theSwitch=true});
customTab cTab;
Int32 z = 0;
foreach (SwitchClass s in col2)
{
cTab = new customTab();
cTab.DataContext = s;
cTab.Header = "Tab " + z.ToString();
z++;
tabControl.Items.Add(cTab);
}
with
<CheckBox Name="theCheckbox" Width="200" Height="50" Content="Checkbox" IsChecked="{Binding theSwitch}" />
works perfectly. I suppose this is just a limitation of WPF?

Related

How to: wpf datagrid totally programmatical, including binding to observablecollection

I have to perform the entire thing in code, and am at loss after spending two says in StackOverflow and dear uncle google.
I have a class TestData:InotifyPropertyChanged which includes a string "Name", another string "Specifics" and an bool array "checks". This class is used to build a static Observablecollection "testData" (lowercase).
I'd like to create by code a datagrid, not let it make automatic columns, and present these columns:
DataGridTextColumn with "Name", non-editable.
DataGridTextColumn with "Specifics", editable (and presenting a default value "-").
Three DataGridCheckBoxColums with the "checks" data.
The strucutre is set-up properly, and displays the required headers and the placeholders for the data. There's a button which adds new records to "testData", and additional rows are indeed added to the datagrid, but sadly these are empty.
I need this to be two-way, obviously.
I can't manage to display the values I have set in the declaration of the "testData" for "specifics" and "checks".
In addition, I keep receiving an error saying that "two way binding requires Path or x:path". It's frustrating...
Here's what I tried:
<DataGrid x:Name="dgMainDataGrid" CanUserAddRows="False" AutoGenerateColumns="False" />
and
dgMainDataGrid.ItemsSource = testData;
DataGridTextColumn col1 = new DataGridTextColumn();
col1.Header = "col1Header";
col1.IsReadOnly = true;
col1.Binding = new Binding("Name");
DataGridTextColumn col2 = new DataGridTextColumn();
col2.Header = "col2Header";
col2.IsReadOnly = false;
col2.Binding = new Binding("Specifics");
DataGridCheckBoxColumn col3 = new DataGridCheckBoxColumn();
col3.Header = "col3Header";
col3.IsReadOnly = false;
col3.Binding = new Binding("checks[0]");
DataGridCheckBoxColumn col4 = new DataGridCheckBoxColumn();
col4.Header = "col3Header";
col4.IsReadOnly = false;
col4.Binding = new Binding("checks[1]");
DataGridCheckBoxColumn col5 = new DataGridCheckBoxColumn();
col5.Header = "col3Header";
col5.IsReadOnly = false;
col5.Binding = new Binding("checks[2]");
and class
public class TestData : INotifyPropertyChanged
{
public string Name;
public string Specifics;
public bool[] checks;
}
Thanks, guys.
After #Clemens asked how my class looks, I saw these were not properties. Just sprinkle some {get; set;} over the fields.

Set DisplayMemberPath and SelectedValue in WPF with Code

I have a DataTable that is coming from a Web Service, which I need to bind to a ComboBox. I have not grokked doing binding in XAML yet so this question is about binding in code instead. So far I have tried
cboManager.DataContext = Slurp.DistrictManagerSelect().DefaultView;
cboManager.DisplayMemberPath = "Name";
cboManager.SelectedValuePath = "NameListId";
cboManager.SetBinding(ComboBox.ItemsSourceProperty, new Binding());
And I have tried
DataTable tbl = new DataTable();
tbl = Slurp.DistrictManagerSelect();
cboManager.ItemsSource = ((IListSource)tbl).GetList();
cboManager.DisplayMemberPath = "[Name]";
cboManager.SelectedValuePath = "[NameListId]";
DataContext = this;
In both cases I get the list of managers to show but when I select from the ComboBox I get [Name] and [NameListId] and not the values I am expecting. What am I doing wrong (other than not using XAML's DataBinding)?
Edit added after answers to my original post came in.
So (based on Rachel's response) try number three looks like this:
using (DataTable tbl = Slurp.DistrictManagerSelect())
{
List<ManagerList> list = new List<ManagerList>();
foreach (var row in tbl.Rows)
{
list.Add(new ManagerList
{
NameListId = (int)row[0],
Name = row[1].ToString()
});
}
}
Assuming I am doing what she meant the correct way I am no getting this error Cannot apply indexing with [] to an expression of type 'object'
Have you tried to just bind to the DataTable directly? Do you have columns Name and NameListId? Leave off the DataContext (you already assigned the ItemsSource).
DataTable tbl = Slurp.DistrictManagerSelect();
cboManager.ItemsSource = tbl;
cboManager.DisplayMemberPath = "Name";
cboManager.SelectedValuePath = "NameListId";
When you cast to the IListSource I suspect it is combining all the columns. If you want to bind to a list then you need to create items that have properties Name and NameListID.
I think that's because your ItemsSource is a DataTable, so each ComboBoxItem contains a DataContext of a DataRow, and the DataRow class doesn't have properties called Name or NameListId
Basically, you're trying to tell the ComboBox to display DataRow.Name, and set the value to DataRow.NameListId, both of which are not valid properties.
I usually prefer to parse data into objects, and bind the ItemsSource a List<MyObject> or ObservableCollection<MyObject>
foreach(DataRow row in tbl.Rows)
list.Add(new MyObject { Name = row[0].ToString(), NameListId = (int)row[1] });
cboManager.ItemsSource = list;

DataSet created in code not Binding to ListView

I have a WPF User Control with a ListView in it that is created based on the DataSet that is passed to it:
public void PopulateList(DataSet ds) {
listView.View = CreateGridViewColumns(ds.Tables[0]);
listData.DataContext = ds.Tables[0];
}
private GridView CreateGridViewColumns(DataTable dt) {
// Create the GridView
var gv = new GridView {AllowsColumnReorder = true};
// Create the GridView Columns
foreach (DataColumn item in dt.Columns) {
var gvc = new GridViewColumn
{
DisplayMemberBinding = new Binding(item.ColumnName),
Header = item.ColumnName,
Width = Double.NaN
};
gv.Columns.Add(gvc);
}
return gv;
}
Now I create the user control in code and call it's PopulateList with the appropriate dataset and this is where the problems are starting:
If I pass in a dataset that was created from a call to the database the list view shows all the data but if i pass in a DataSet that i created in code the ListView shows the Columns but will not show the data
//This is a function that hides the DB call return type is DataSet
var dsPatientSmokingStatusHistory = DataRepository.PatientSmokingStatusProvider.GetHistory(PatientId);
//radGridViewPatientSmokingStatus.DataSource = dsPatientSmokingStatusHistory.Tables[0];
var dt = new DataTable();
string c1 = "Date".PadLeft(23).PadRight(23);
string c2 = "Status".PadLeft(20).PadRight(50);
dt.Columns.Add(c1);
dt.Columns.Add(c2);
int i = 0;
foreach (DataRow row in dsPatientSmokingStatusHistory.Tables[0].Rows) {
var dataRow = dt.NewRow();
dataRow[c1] = ((DateTime)row["Date"]).ToString("MM/dd/yyyy");
dataRow[c2] = row["Status"].ToString();
dt.Rows.Add(dataRow);
dt.Rows[i].AcceptChanges();
i++;
}
DataSet ds = new DataSet();
dt.TableName = "Table";
ds.Tables.Add(dt);
ds.AcceptChanges();
smokingStatusGrid.GridWidth = 455;
smokingStatusGrid.GridHight = 97;
//This line does not show data
smokingStatusGrid.PopulateGrid(ds);
//This line will show data
smokingStatusGrid.PopulateGrid(dsPatientSmokingStatusHistory);
Is there a difference between these two datasets that i don't know about that is preventing me from databinding to it?
Also the user control is being used as an ElementHost in a WinForms application (not sure if this makes a difference)
Your code says:
DisplayMemberBinding = new Binding(item.ColumnName)
This binding constructor takes a string paramter which as per MSDN is "The initial Path for the binding" and is of datatype System.Windows.PropertyPath. I guess, since system tries to find a property with the same name in your class, and your string (item.ColumnName) has spaces at start, it runs into a problem (properties can't start with a space).
Would recommend you to take off the padding that you are doing in column name of your table. Apply any padding/margins in the Header of your GridView.

Databinding of DataGridViewComboBoxColumn

In the code below, the combo box named "ConnectionType" shows the selected item, but one cannot change the selected item (it seems like there is only one item in the combo box). If I comment out the line
typeCol.DataPropertyName = "ConnectionTypeName";
then the combo box is selectable, but the correct item is not selected, of course. What am I doing wrong??
Thanks.
private void LoadConnectionsGrid()
{
_dc = new EnterpriseEntities();
dataGridViewConnections.AutoGenerateColumns = false;
dataGridViewConnections.DataSource = _dc.Connection.Include("ConnectionType");
DataGridViewComboBoxColumn typeCol =
(DataGridViewComboBoxColumn)dataGridViewConnections.Columns["ConnectionType"];
typeCol.DataPropertyName = "ConnectionTypeName";
var qry = from c in _dc.ConnectionType
select c.Type;
typeCol.DataSource = qry;
DataGridViewTextBoxColumn nameCol =
(DataGridViewTextBoxColumn)dataGridViewConnections.Columns["ConnectionName"];
nameCol.DataPropertyName = "Name";
DataGridViewTextBoxColumn connStrCol =
(DataGridViewTextBoxColumn)dataGridViewConnections.Columns["ConnectionString"];
connStrCol.DataPropertyName = "ConnectionString";
}
Ultimately, I could not get data binding, the entity framework, and comboboxes to play nice and I just created the data grid brute force (code below). This means that I handle inserts, updates and deletes by hand.
private void LoadConnectionsGrid()
{
DataGridViewComboBoxColumn typeCol =
(DataGridViewComboBoxColumn)dataGridViewConnections.Columns["ConnectionType"];
var qry = from c in _dc.ConnectionType
select c.Type;
typeCol.DataSource = qry;
dataGridViewConnections.Rows.Clear();
foreach (Connection conn in _dc.Connection.Include("ConnectionType"))
{
dataGridViewConnections.Rows.Add(conn.Name,
conn.ConnectionType.Type, conn.ConnectionString);
}
}

How to maintain ComboBox.SelectedItem reference when DataSource is resorted?

This really seems like a bug to me, but perhaps some databinding gurus can enlighten me? (My WinForms databinding knowledge is quite limited.)
I have a ComboBox bound to a sorted DataView. When the properties of the items in the DataView change such that items are resorted, the SelectedItem in my ComboBox does not keep in-sync. It seems to point to someplace completely random. Is this a bug, or am I missing something in my databinding?
Here is a sample application that reproduces the problem. All you need is a Button and a ComboBox:
public partial class Form1 : Form
{
private DataTable myData;
public Form1()
{
this.InitializeComponent();
this.myData = new DataTable();
this.myData.Columns.Add("ID", typeof(int));
this.myData.Columns.Add("Name", typeof(string));
this.myData.Columns.Add("LastModified", typeof(DateTime));
this.myData.Rows.Add(1, "first", DateTime.Now.AddMinutes(-2));
this.myData.Rows.Add(2, "second", DateTime.Now.AddMinutes(-1));
this.myData.Rows.Add(3, "third", DateTime.Now);
this.myData.DefaultView.Sort = "LastModified DESC";
this.comboBox1.DataSource = this.myData.DefaultView;
this.comboBox1.ValueMember = "ID";
this.comboBox1.DisplayMember = "Name";
}
private void saveStuffButton_Click(object sender, EventArgs e)
{
DataRowView preUpdateSelectedItem = (DataRowView)this.comboBox1.SelectedItem;
// OUTPUT: SelectedIndex = 0; SelectedItem.Name = third
Debug.WriteLine(string.Format("SelectedIndex = {0:N0}; SelectedItem.Name = {1}", this.comboBox1.SelectedIndex, preUpdateSelectedItem["Name"]));
this.myData.Rows[0]["LastModified"] = DateTime.Now;
DataRowView postUpdateSelectedItem = (DataRowView)this.comboBox1.SelectedItem;
// OUTPUT: SelectedIndex = 2; SelectedItem.Name = second
Debug.WriteLine(string.Format("SelectedIndex = {0:N0}; SelectedItem.Name = {1}", this.comboBox1.SelectedIndex, postUpdateSelectedItem["Name"]));
// FAIL!
Debug.Assert(object.ReferenceEquals(preUpdateSelectedItem, postUpdateSelectedItem));
}
}
To clarify:
I understand how I would fix the simple application above--I only included that to demonstrate the problem. My concern is how to fix it when the updates to the underlying data rows could be happening anywhere (on another form, perhaps.)
I would really like to still receive updates, inserts, deletes, etc. to my data source. I have tried just binding to an array of DataRows severed from the DataTable, but this causes additional headaches.
Just add a BindingContext to the ComboBox :
this.comboBox1.DataSource = this.myData.DefaultView;
this.comboBox1.BindingContext = new BindingContext();
this.comboBox1.ValueMember = "ID";
this.comboBox1.DisplayMember = "Name";
By the way, try not keeping auto-generated names for your widgets (comboBox1, ...), it is dirty. :-P
The only promising solution I see at this time is to bind the combo box to a detached data source and then update it every time the "real" DataView changes. Here is what I have so far. Seems to be working, but (1) it's a total hack, and (2) it will not scale well at all.
In form declaration:
private DataView shadowView;
In form initialization:
this.comboBox1.DisplayMember = "Value";
this.comboBox1.ValueMember = "Key";
this.shadowView = new DataView(GlobalData.TheGlobalTable, null, "LastModified DESC", DataViewRowState.CurrentRows);
this.shadowView.ListChanged += new ListChangedEventHandler(shadowView_ListChanged);
this.ResetComboBoxDataSource(null);
And then the hack:
private void shadowView_ListChanged(object sender, ListChangedEventArgs e)
{
this.ResetComboBoxDataSource((int)this.comboBox1.SelectedValue);
}
private void ResetComboBoxDataSource(int? selectedId)
{
int selectedIndex = 0;
var detached = new KeyValuePair<int, string>[this.shadowView.Count];
for (int i = 0; i < this.shadowView.Count; i++)
{
int id = (int)this.shadowView[i]["ID"];
detached[i] = new KeyValuePair<int, string>(id, (string)this.shadowView[i]["Name"]);
if (id == selectedId)
{
selectedIndex = i;
}
}
this.comboBox1.DataSource = detached;
this.comboBox1.SelectedIndex = selectedIndex;
}
Must detach event handler in Dispose:
this.shadowView.ListChanged -= new ListChangedEventHandler(shadowView_ListChanged);
Your example sorts the data on the column it updates. When the update occurs, the order of the rows changes. The combobox is using the index to keep track of it's selected items, so when the items are sorted, the index is pointing to a different row. You'll need to capture the value of comboxBox1.SelectedItem before updating the row, and set it back once the update is complete:
DataRowView selected = (DataRowView)this.comboBox1.SelectedItem;
this.myData.Rows[0]["LastModified"] = DateTime.Now;
this.comboBox1.SelectedItem = selected;
From an architecture perspective, the SelectedItem must be cleared when rebinding the DataSource because the DataBinder don't know if your SelectedItem will persist or not.
From a functional perspective, the DataBinder may not be able to ensure that your SelectedItem from you old DataSource is the same in your new DataSource (it can be a different DataSource with the same SelectedItem ID).
Its more an application feature or a custom control feature than a generic databinding process.
IMHO, you have theses choices if you want to keep the SelectedItem on rebind :
Create a reusable custom control / custom DataBinder with a persistance option which try to set the SelectedItem with all your data validation (using a DataSource / item identification to ensure the item validity)
Persist it specifically on your Form using the Form/Application context (like ViewState for ASP.NET).
Some controls on the .NET market are helping you by rebinding (including selections) the control from their own persisted DataSource if the DataSource is not changed and DataBind not recalled. That's the best pratice.

Resources