2013-03-18 193 views
1

我遇到了很多麻烦,试图将我的控件绑定到数据源。我尝试绑定到XML文档。这很有效,但是当我尝试刷新XML文档并更新UI时遇到了很多问题。WPF绑定控件到DataView

我最新的尝试是将我的控件绑定到DataView,这看起来很简单。我有一个示例应用程序,我发现这里StackOverflow上,这确实这样:

public MainWindow() 
    { 

     InitializeComponent(); 

     DataTable dataTable = GetTable(); 
     Binding dataTableBinding = new Binding(); 
     dataTableBinding.Source = dataTable; 
     dataTableBinding.Path = new PropertyPath("Rows[0][MyTextColumn]"); 
     txtMyTextColumnDataTable.SetBinding(TextBox.TextProperty, dataTableBinding); 

     DataView dataView = dataTable.DefaultView; 
     Binding dataViewBinding = new Binding(); 
     dataViewBinding.Source = dataView; 
     dataViewBinding.Path = new PropertyPath("[0][MyTextColumn]"); 
     txtMyTextColumnDataView.SetBinding(TextBox.TextProperty, dataViewBinding); 
    } 

这工作完全,正确的开箱即用。我添加了一个代码更新数据表中的值的按钮,当我单击该按钮时,文本框会立即反映新值。

我想这在我的VB.Net项目,像这样:

dim plcData As DataTable = GetTable() 
    dim plcView As DataView = plcData.DefaultView 
    dim plcBinding As Binding = New Binding 
    plcBinding.Source = plcView 
    plcBinding.Path = New PropertyPath("(0)(conveyor_plc_data_Main_FeedCarousel_caroAngle)") 
    Me.tb.SetBinding(TextBlock.TextProperty, plcBinding) 

而且这是行不通的。它不会更新我的UI控件。 在这两种情况下,GetTable都会用样本数据构建一行DataTable。在我的VB项目中,tb是我的MainWindow上的一个TextBlock。

在VB项目中,我可以打断我的代码并查询立即窗口中的特定数据列,并且正确的值在那里。它只是不会更新到我的控制。

这似乎是一件非常简单的事情。我对WPF相当陌生,看不出我的代码有什么问题。最终我想在我的XAML中定义绑定,但无法弄清楚如何做到这一点。此时,绑定的代码隐藏设置就可以了。我将有许多控件绑定到许多数据列。

有人可以告诉我我在这里失踪了什么明显的东西吗?

+0

如果您使用与C#相同的属性路径,VB版本是否工作? 'plcBinding.Path =新的PropertyPath(“[0] [conveyor_plc_data_Main_FeedCarousel_caroAngle]”)' – 2013-03-18 19:07:59

+0

OMG,这是愚蠢的简单?在我的辩护中,我认为这是正确的VB语法,通常使用parens而不是括号。 – RMittelman 2013-03-18 19:19:59

+0

当我查询立即窗口中的值时,方括号不起作用:?plcView [0] [“conveyor_plc_data_Main_FeedCarousel_caroAngle”] 标识符预期。 – RMittelman 2013-03-18 19:21:39

回答

2

根据the documentationPropertyPath类的语法只接受C#风格的索引器。

单索引的直接对象是数据上下文:

<Binding Path="[key]" .../> 

类没有办法根据主叫语言来改变它的语法。


编辑
设置当创建DataView代码的背后,暴露的视图属性在XAML绑定:

public static readonly DependencyProperty plcViewProperty 
    = DependencyProperty.Register("plcView", typeof(System.Data.DataView), 
    typeof(MainWindow), new PropertyMetadata(null)); 

public System.Data.DataView plcView 
{ 
    get { return (System.Data.DataView)GetValue(plcViewProperty); } 
    set { SetValue(plcViewProperty, value); } 
} 

private void MainWindow_Initialized(object sender, EventArgs eventArgs) 
{ 
    plcView = GetTable().DefaultView; 
} 

然后在您的XAML:

<Window x:Name="TheWindow" ...> 
    ... 
    Text="{Binding ElementName=TheWindow, 
     Path=plcView[0][conveyor_plc_data_Main_FeedCarousel_caroAngle]}" 
+0

在我看到这个之前,我在TextBox的XAML中尝试了MainWindow_Initialized中的Me.DataContext = plcView',然后在TextBox的XAML中使用'Text =“{Binding Path = [0] [conveyor_plc_data_Main_FeedCarousel_caroAngle]}”'(不带DataView名称)。没有宣布财产的作品。仍然无法在XAML中声明DC,但没关系。 – RMittelman 2013-03-18 20:21:04

+0

现在最大的问题是:我可以在代码中删除plcView中的行,然后添加一个新的行0(带有新值),绑定会自动导致所有值更新? – RMittelman 2013-03-18 20:22:55

+0

我添加了一个属性,因为我不确定你是否已经有了'DataContext'集合。如果您更改了该行中的数据,或者替换了该行,则需要为该属性引发一个合适的“PropertyChanged”事件。尝试将'DataContext'设置为'null',然后返回到'DefaultView'。 – 2013-03-18 20:56:00