Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7877725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:27:19+00:00 2026-06-03T03:27:19+00:00

NARROWED DOWN SOLUTION I’m much closer, but don’t know how to apply XAML to

  • 0

NARROWED DOWN SOLUTION

I’m much closer, but don’t know how to apply XAML to change datacontext value. Please review context of original question below as may be needed.

My issue is that I have a ViewModel class as the datacontext to a window. On this view model, I have a “DataTable” object (with columns and just a single row for testing). When I try to set a Textbox “TEXT” binding to the column of the datatable, it doesn’t work. What I’ve ultimately found is that no matter what “source” or “path” I give it, it just won’t cooperate. HOWEVER, just by playing around with scenarios, I said the heck with it. Lets look. The Textbox control has its own “DataContext” property. So, in code, I just FORCED the textbox.DataContext = “MyViewModel.MyDataTableObject” and left the path to just the column it should represent “MyDataColumn”, and it worked.

So, that said, how would I write the XAML for the textbox control so it’s “DataContext” property is set to that of the datatable object of the view model the window but can’t get that correct. Ex:

<TextBox Name="myTextBox" 
    Width="120"
    DataContext="THIS IS WHAT I NEED" --- to represent
    Text="{Binding Path=DataName, 
                    ValidatesOnDataErrors=True,
                    UpdateSourceTrigger=PropertyChanged }" />

DataContext for this textbox should reflect XAML details below and get

(ActualWindow) ( DDT = View Model) (oPerson = DataTable that exists ON the view model)
CurrentWindow.DDT.oPerson

I’m stuck on something with binding. I want to bind a column of a datatable to a textbox control. Sounds simple, but I’m missing something. Simple scenario first. If I have my window and set the data context to that of “MyDataTable”, and have the textbox PATH=MyDataColumn, all works fine, no problems, including data validation (red border on errors).

Now, the problem. If I this have a same “MyDataTable” as a public on my Window Class directly (but same thing if I had it on an actual ViewModel object, but the window to simplify the level referencing), I can’t get it to work from direct XAML source. I knew I had to set the “SOURCE=MyDataTable”, but the path of just the column didn’t work.

<TextBox Name="myTextBox" 
         Text="{Binding  Source=DDT, Path=Rows[0][DataName], 
                         ValidatesOnDataErrors=True,
                         UpdateSourceTrigger=PropertyChanged }" />

However, from other testing, if I set the path (in code-behind) to

object txt = FindName("myTextBox");
Binding oBind = new Binding("DataName");
oBind.Source = DDT;
oBind.Mode = BindingMode.TwoWay;
oBind.ValidatesOnDataErrors = true;
oBind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
((TextBox)txt).SetBinding(TextBox.TextProperty, oBind);

It DOES work (when the datatable is available as public in the window (or view model))

What am I missing otherwise.

UPDATE: HERE IS A FULL POST of the sample code I’m applying here.

using System.ComponentModel;
using System.Data;

namespace WPFSample1
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public DerivedDataTable DDT;

    public MainWindow()
    {
      InitializeComponent();
      // hook up to a Data Table 
      DDT = new DerivedDataTable();
      DataContext = this;

      // with THIS part enabled, the binding works.  
      // DISABLE this IF test, and binding does NOT.
      // but also note, I tried these same settings manually via XAML.
      object txt = FindName("myTextBox");
      if( txt is TextBox)
      {
        Binding oBind = new Binding("DataName");
        oBind.Source = DDT;
        oBind.Mode = BindingMode.TwoWay;
        oBind.ValidatesOnDataErrors = true;
        oBind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        ((TextBox)txt).SetBinding(TextBox.TextProperty, oBind);
      }
    }
  }

  // Generic class with hooks to enable error trapping at the data table
  // level via ColumnChanged event vs IDataErrorInfo of individual properties
  public class MyDataTable : DataTable
  {
    public MyDataTable()
    {
      // hook to column changing
      ColumnChanged += MyDataColumnChanged;
    }

    protected void MyDataColumnChanged(object sender, DataColumnChangeEventArgs e)
    { ValidationTest( e.Row, e.Column.ColumnName); }

    // For any derived datatable to just need to define the validation method
    protected virtual string ValidationTest(DataRow oDR, string ColumnName)
    { return ""; }
  }

  public class DerivedDataTable : MyDataTable
  {
    public DerivedDataTable()
    {
      // simple data table, one column, one row and defaulting the value to "X"
      // so when the window starts, I KNOW its properly bound when the form shows
      // "X" initial value when form starts
      Columns.Add( new DataColumn("DataName", typeof(System.String))  );
      Columns["DataName"].DefaultValue = "X";

      // Add a new row to the table
      Rows.Add(NewRow());
    }

    protected override string ValidationTest(DataRow oDR, string ColumnName)
    {
      string error = "";
      switch (ColumnName.ToLower())
      {
        case "dataname" :
          if (   string.IsNullOrEmpty(oDR[ColumnName].ToString() )
            || oDR[ColumnName].ToString().Length < 4 )
            error = "Name Minimum 4 characters";

          break;
      }

      // the datarow "SetColumnError" is what hooks the "HasErrors" validation
      // in similar fashion as IDataErrorInfo.
      oDR.SetColumnError(Columns[ColumnName], error);

      return error;
    }
  }
}

AND here’s the XAML. Any brand new form and this is the only control in the default “grid” of the window.

Tried following versions, just defining the Rows[0][Column]

<TextBox Name="myTextBox" 
    Width="120"
    Text="{Binding  Path=Rows[0][DataName], 
                    ValidatesOnDataErrors=True,
                    UpdateSourceTrigger=PropertyChanged }" />

Including the source of “DDT” since it is public to the window

<TextBox Name="myTextBox" 
    Width="120"
    Text="{Binding  Source=DDT, Path=Rows[0][DataName], 
                    ValidatesOnDataErrors=True,
                    UpdateSourceTrigger=PropertyChanged }" />

And even suggestions offered by grantnz

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:27:21+00:00Added an answer on June 3, 2026 at 3:27 am

    SOLVED, but what a PITA… Most things within the samples of doing MVVM patterns will have properties on the view model exposing whatever you want to hook into. When dealing with binding to a DATATABLE (or similar view, etc), you are binding to COLUMNs of said table (or view).

    When a table is queried from whatever back-end, the schema populating the data columns will always force the column names to UPPER CASE.

    So, if you have a column “InvoiceTotal” in your table, when queried, the column name will have it as “INVOICETOTAL”.

    If you try to bind to the

    Path="InvoiceTotal" ... it will fail
    
    Path="INVOICETOTAL" ... it WILL WORK
    

    However, if you are working directly in .Net (I use C#), the following will BOTH get a value back from the row

    double SomeValue = (double)MyTable.Rows[0]["InvoiceTotal"];
    or
    double SomeValue = (double)MyTable.Rows[0]["INVOICETotal"];
    or
    double SomeValue = (double)MyTable.Rows[0]["invoicetotal"];
    

    all regardless of the case-sensitivity of the column name.

    So, now the rest of the bindings, Error triggers available at the table, row or column levels can properly be reflected in the GUI to the user.

    I SURE HOPE this saves someone else the headaches and research I have gone through on this….

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ive narrowed down to this method but i don't understand why its locking the
I have narrowed down my issue to a derived classes copy constructor, but I
i narrowed down what i want my wpf button to look like using XAML.
I've narrowed down my problem to these lines of code and I know it
I have narrowed down my problem to the mysql_connect call I am doing in
I've narrowed down in my application that my AVI video player is leaking memory.
I've editing this original question as I think I've narrowed down the problem... I
With Java and Perl background, I have narrowed down the web framework choices to
I've done my research and narrowed this down. OK, so I am deciding on
I've narrowed the problem down to the following simple code snippet: #!/usr/bin/env ruby print

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.