将选定的 DataGrid 行绑定到文本框

汉斯

我想将文本框绑定到选定的DataGrid. 我已经将列表绑定到数据网格,但现在我想将TextBox文本绑定DataGrid所选行,以便将其内容放入TextBox

txtOccArea.DataContext = hegData;
//hegData is a list of an object 

谢谢!

胡安·卡洛斯·罗德里格斯

您应该创建一个这样的新类(我希望您使用的是 MVVM)。

public class YourViewVM : INotifyPropertyChanged
{

    #region Fields

    private object selectedDataGridCell;
    private string textBoxContent;
    private List<YourObject> dataGridSource;

    #endregion

    #region Properties
    public object SelectedDataGridCell
    {
        get
        {
            return this.selectedDataGridCell;

        }
        set
        {
            if (this.selectedDataGridCell != value)
            {
                this.selectedDataGridCell = value;
                OnPropertyChanged("SelectedDataGridCell");
            }
        }
    }


    public string TextBoxContent
    {
        get
        {
            return this.textBoxContent;
        }
        set
        {
            if (this.textBoxContent != value)
            {
                this.textBoxContent = value;
                OnPropertyChanged("TextBoxContent");
            }
        }
    }

    public List<YourObject> DataGridSource
    {
        get
        {
            return this.dataGridSource;
        }
        set
        {
            if (this.dataGridSource != value)
            {
                this.dataGridSource = value;
                OnPropertyChanged("Source");
            }
        }

    }

    #endregion

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

    }
}

在您看来,只需将其修改为:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <DataGrid ItemsSource="{Binding DataGridSource}" SelectedItem="{Binding SelectedDataGridCell}" />
    <TextBox Grid.Row="1" Text="{Binding TextBoxContent}"></TextBox>
</Grid>

您需要添加INotifyPropertyChanged以便TextBox知道选择何时更改。

如果您需要将 DataGridSource 设置为您的hegData列表,只需创建一个构造函数并在那里设置属性,如下所示:

public YourViewVM(List<YourObject> hegData)
    {
        this.DataGridSource = hegData;
    }

在您创建它的地方,只需将其命名为:

YourViewVM yourViewVM = new YourViewVM(hegData)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将选定的datagrid行加载到标签

来自分类Dev

将文本框绑定到属性

来自分类Dev

将文本框绑定到字典

来自分类Dev

将文本框绑定到属性

来自分类Dev

将属性绑定到 Datagrid 上的文本框在 setter 例程后不显示值

来自分类Dev

从Datagrid(WPF)绑定文本框

来自分类Dev

将文本从文本框发送到datagrid

来自分类Dev

TextBlock绑定到选定的DataGrid元素

来自分类Dev

将列表绑定到DataGrid

来自分类Dev

将datagrid绑定到datareader

来自分类Dev

将customObject绑定到DataGrid

来自分类Dev

将ObservableCollection绑定到DataGrid

来自分类Dev

将Datagrid绑定到XMLDatasource

来自分类Dev

将customObject绑定到DataGrid

来自分类Dev

WPF DataGrid获取选定的行

来自分类Dev

WPF DataGrid获取选定的行

来自分类Dev

从文本框值填充datagrid行?

来自分类Dev

如何将文本框绑定到类Property

来自分类Dev

TwoWay将DateTime绑定到文本框-仅年份

来自分类Dev

将多个文本框值绑定到单个属性

来自分类Dev

如何将文本框对象绑定到ViewModel

来自分类Dev

将文本框绑定到字典的键

来自分类Dev

将List <string>绑定到文本框

来自分类Dev

将文本框值绑定到WPF中的模型

来自分类Dev

将DoubleClick命令从DataGrid行绑定到VM

来自分类Dev

将ComboBox SelectedItem绑定到Datagrid

来自分类Dev

将ContextMenu绑定到Datagrid列

来自分类Dev

将ComboBox绑定到DataGrid条目

来自分类Dev

将项目列表绑定到Datagrid单元