通过代码绑定绑定到TextBox [英] Binding To a TextBox Through Code-Behind

查看:99
本文介绍了通过代码绑定绑定到TextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGrid ,我订阅了 SelectedCellsChanged 事件。如果选择的单元格数量只有一个,我想在带有TwoWay Binding的 TextBox 中显示特定单元格的文本。但是如果所选单元格的数量超过一个,我希望 TextBox 变为空,并且它的绑定变为null。



任何人都可以帮我吗?

附加数据:

DataGrid绑定到

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

ObservableCollection<MyClass> MyRowList;



我知道选择了哪个单元格,我知道属性的名称。我的属性名称类似于Column1,Column2,Column3等。因此,如果选择第三行的第三个单元格,相应的属性将是 MyRowList [rowIndex] .Column3



我假设语法看起来像这样:


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:

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;
        }

推荐答案

在WPF中,绑定需要三件事:



  • 目标
  • 来源
  • A Binding
In WPF, three things are needed for a binding:

  • A target
  • A source
  • A Binding
// 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.

Binding b = new Binding();
b.Mode = BindingMode.OneWay;
b.Path = "FakeProperty";
MyTextbox.SetBinding(TextBox.TextProperty, b);
MyTextbox.IsEnabled = false;


这篇关于通过代码绑定绑定到TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆