RMittelman
RMittelman

Reputation: 317

WPF Bind control to DataView

I am having a LOT of trouble trying to bind my controls to a data source. I tried binding to XML document. That worked, but lots of issues when I tried to refresh the XML document itself and have it update the UI.

My newest try is to bind my controls to a DataView, which seems simple. I have a sample app I found here on StackOverflow, which does this:

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

This works perfectly, right out of the box. I added a button whose code updates the value in the data table, and the textbox immediately reflects the new value when I click that button.

I tried this in my VB.Net project, like this:

    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)

And it doesn't work. It will not update my UI control. In both cases, GetTable builds a 1-row DataTable with sample data. In my VB project, tb is a TextBlock on my MainWindow.

In the VB project, I can interrupt my code and query the particular data column in the Immediate window, and the proper value is there. It just won't update into my control.

This seems like a very simple thing to do. I am quite new to WPF, and can't see what is wrong with my code. Eventually I would like to define the binding in my XAML, but can't figure out how to do this. At this point, a code-behind setting of the binding would be ok. I will have many controls to be bound to many data columns.

Can anybody tell me what obvious thing I'm missing here?

Upvotes: 1

Views: 3653

Answers (1)

Richard Deeming
Richard Deeming

Reputation: 31208

According to the documentation, the syntax for the PropertyPath class only accepts C#-style indexers.

Single Indexer on the Immediate Object as Data Context:

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

The class has no way to change its syntax based on the calling language.


EDIT
To set the binding in XAML when the DataView is created in the code-behind, expose the view as a property:

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

Then in your XAML:

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

Upvotes: 2

Related Questions