WPF Databinding Textbox to DataTable

Mark Wiegold 1 Reputation point
2022-03-02T16:38:55.017+00:00

Hi All,

I'm having a problem trying to get a Textbox to update a datatable cell. The binding works correctly oneway, but it does not update the table when the UI changes.
I am using VS2019 WPF/C#

Binding Code Example:

    <TextBox  Grid.Column="2" Grid.Row="2" Margin="5,5" ToolTip="{Binding Parameters[41]}" x:Name="DiameterLowerRoll1">
        <Binding Path="CreatedRecipe.RecipeData.Rows[41][2]" UpdateSourceTrigger="PropertyChanged"  Mode="TwoWay">
            <Binding.ValidationRules>
                <Validate:RangeValidationRule Minimum="300" Maximum="650"  ValidatesOnTargetUpdated="True"/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox>

The ViewModel parameter is bound to a custom class:

    public Recipe CreatedRecipe
    {
        get { return createdRecipe; }
        set {
            createdRecipe = value;
            OnPropertyChanged(nameof(CreatedRecipe));
            }
    }

As I said before, this works correctly when the Recipe is set. I'm not too fussed about property changed firing for the textbox changing, but I do need the data to be recorded in the table.

Any help is appreciated.

Regards

XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
808 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,491 Reputation points Microsoft Vendor
    2022-03-03T08:23:53.117+00:00

    @Mark Wiegold , Based on your description, you want to update the datatable when the textbox's property ToolTip is changed.

    I used the following code to do it successfully, but I didn't use MVVM to do it.

      public partial class MainWindow : Window  
        {  
         
            DataTable table = new DataTable();  
            public MainWindow()  
            {  
                InitializeComponent();  
      
                table.Columns.Add("FirstName");  
                table.Columns.Add("Address");  
                table.Columns.Add("Description");  
                table.Rows.Add("test1", "Home", "d1");  
                table.Rows.Add("test2", "Company", "d2");  
                table.Rows.Add("test3", "Home", "d3");  
                table.Rows.Add("test4", "Home", "d4");  
      
                Binding bindMyColumn = new Binding();  
                bindMyColumn.Source = table;  
                bindMyColumn.Path = new PropertyPath("Rows[2][0]");  
                bindMyColumn.Mode = BindingMode.TwoWay;  
                DiameterLowerRoll1.SetBinding(TextBox.ToolTipProperty, bindMyColumn);  
              
      
            }  
      
            private void btntest_Click(object sender, RoutedEventArgs e)  
            {  
                DiameterLowerRoll1.ToolTip = "test3333";  
               
            }  
      
             
        }  
    

    Result:

    179572-8.gif

    Hope mu solution could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.