【问题标题】:How do I bind a TextBox to a DataTable in XAML?如何在 XAML 中将 TextBox 绑定到 DataTable?
【发布时间】:2014-03-20 17:45:49
【问题描述】:

我有一个 WPF 窗口。它的 DataContext 设置为一个 ViewModel,其中包含一个作为名为 dtPatientData 的属性公开的 DataTable。我有一个数据网格(ComponentOne),它的 ItemsSource 设置为此属性。数据网格正确绑定表并显示它。我还希望有一个 TextBox 绑定到当前选定行中特定列(MRN)的值。我失败了。这是我的 XAML:

                 <C1DataGrid:C1DataGrid
                    HorizontalAlignment="Left"
                    Margin="10,0,0,10"
                    VerticalAlignment="Bottom"
                    Width="504"
                    Height="143"
                    ItemsSource="{Binding dtPatientData}"
                    />
                <TextBox
                    Text="{Binding Source=dtPatientData, Path=MRN}"/>

我得到以下信息:System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“字符串”(HashCode=1384163063)上找不到“MRN”属性。绑定表达式:路径=MRN; DataItem='String' (HashCode=1384163063);目标元素是'TextBox'(名称='');目标属性是“文本”(类型“字符串”)

我尝试了各种方法。如何在 XAML 中执行此操作?

【问题讨论】:

  • 但是一列可以有零行或多行。
  • 你的 C1DataGrid 有 SelectedItem 属性吗?

标签: c# wpf xaml data-binding


【解决方案1】:

您应该将数据绑定到DataGrid.SelectedItemMRN 列的值。你可以这样做:

<C1DataGrid:C1DataGrid Name="DataGrid" HorizontalAlignment="Left" Margin="10,0,0,10"
    VerticalAlignment="Bottom" Width="504" Height="143" 
    ItemsSource="{Binding dtPatientData}" />

...

<TextBox Text="{Binding SelectedItem.MRN, ElementName=DataGrid}"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-05
    • 2011-08-17
    • 2018-11-09
    • 1970-01-01
    • 2019-06-08
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多