Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a DataGrid and I have subscribed to the SelectedCellsChanged Event. If the number of cells selected is only one, I want to display the text of the particular cell in a TextBox with TwoWay Binding. But if the number of selected cells is more than one, I want the the TextBox to become empty and its binding to become null.

Can anyone help me with this?
Additional Data:
The DataGrid is bound to
C#
ObservableCollection<MyClass> MyRowList;

I know which cell is selected and I know the Name of the property. My property names go like this Column1, Column2, Column3 and so on. So if the 3rd cell of the third row is selected, the corresponding property would be MyRowList[rowIndex].Column3

I am assuming that the syntax would look something like this:
C#
private void DataGridControl_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            int rowIndex, columnIndex;
            System.Windows.Controls.DataGrid tempDG = sender as System.Windows.Controls.DataGrid;
            if (tempDG.SelectedCells.Count == 0)
            {
                return;
            }
            else if (tempDG.SelectedCells.Count == 1)
            {
                rowIndex = getRowIndex; //assume that this works as the real method is a bit long
                columnIndex = getColumnIndex; 
                //set binding here to textbox
                        MyTextBox.Text = new Binding("MyRowList[" + rowIndex.ToString() + "].Column" + columnIndex.ToString); //I am not sure of the syntax, need help here
        }
        else if(tempDG.SelectedCells.Count > 1)
        {
        MytextBox.Binding = null;
        }
Posted
Updated 27-May-13 23:02pm
v2

1 solution

In WPF, three things are needed for a binding:

  • A target
  • A source
  • A Binding

It doesn't matter if you are doing this in XAML or in the code behind file, the concept is the same. In your case the target is the text property of TextBox Mytextbox, and the source is a single property of the selected item in you data grid.

To set the binding, we create an instance of the Binding class; set its Mode to TwoWay. We then set the source of the binding. We'll do this be telling the binding to look for a control with the name [information wasn't given in question]. This name should be the one in the DataGrid's x:Name property. Instead of targeting a cell in the DataGrid, we target the property you want from the item that is selected. We then finish this off by setting the binding to the TextBox's TextProperty attribute.
C#
// The binding
Binding b = new Binding();
b.Mode = BindingMode.TwoWay;

// The source
b.Path = new PropertyPath("SelectedValue.[The MyClass property I'm binding too]");
b.ElementName = "The x:Name of the datagrid.";

// The target
Mytextbox.SetBinding(TextBox.TextProperty, b);

Removing a one way binding can be done by setting a value on the target, but this doesn't work on a two way binding. So instead, I would advise you to set a one way binding to a property that doesn't exist, and disable the textbox.
C#
Binding b = new Binding();
b.Mode = BindingMode.OneWay;
b.Path = "FakeProperty";
MyTextbox.SetBinding(TextBox.TextProperty, b);
MyTextbox.IsEnabled = false;
 
Share this answer
 
Comments
rohith naik 29-May-13 1:13am    
"The x:Name of the datagrid." what do u mean by that. Could you please help me with more specific syntax as I am still a bit confused ? What is Element Name and how is it different from path ??
Francisco T. Chavez 29-May-13 3:25am    
In XAML, you can set the name of a control with x:Name.

For instance, lets say I have a button that I use to start a race car animation, I might want to name that button AnimationStarter. So my XAML might look something like:

<Button x:Name="AnimationStarter"
Content="Start Your Engine"
.../>

Once I've set the name of the button (or any other control outside of a template), I can point to it from within a binding by placing that control's name inside the bindings ElementName property.
rohith naik 29-May-13 1:15am    
Furthur, I need two way binding because I can keep changing the cell I checked and the Textbox has to reflect the new value in the newly selected cell. Thanks for your help.
Francisco T. Chavez 29-May-13 3:37am    
I'm sorry if I wasn't clear about where I was advising the use of the one way binding. What I meant was that when the text box isn't supposed to be bound to an item, I would advise setting a one way binding to a fake property. At that time, you don't need a two way binding, because there isn't suppose to be a binding.

The thing is, I don't know how to get rid of a two way binding. The only reason I know how to get rid of a one way binding is because a coworker of mine ended up setting the value of control's property when that property was bound with a one way binding. I spent nearly two hours trying to figure out why the binding wasn't updating the property value for that control. This is also the reason I try not to name a control unless I have to. Naming a control makes it easier to play with it from inside the code behind file. Of course, there are times when the only easy (or quick) way to do something is by doing it in the code behind file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900