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 7771221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:35:49+00:00 2026-06-01T16:35:49+00:00

I have a WPF textbox with a binding to the datacontext. <TextBox Grid.Column=1 Grid.Row=4

  • 0

I have a WPF textbox with a binding to the datacontext.

<TextBox Grid.Column="1" Grid.Row="4" Text="{Binding Path=Density,UpdateSourceTrigger=PropertyChanged}"/>

I set the datacontext in the code of a the container control of the textbox (tabItem in this case)

tiMaterial.DataContext = _materials[0];

I also have a listbox with other materials. I want to update the textfield, when another material is selected, hence I code:

private void lbMaterials_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    _material = (Material) lbMaterials.SelectedValue;
    tiMaterial.DataContext = _material;            
}

The Material class implements the INotifyPropertyChanged interface. I have the two-way update working, it’s just when I change the DataContext, the bindings seem to be lost.

What am I missing?

  • 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-01T16:35:50+00:00Added an answer on June 1, 2026 at 4:35 pm

    I tryed to do what you describe in your post but sincerely I didn’t find the problem. In all the cases that I tested my project works perfectly.
    I don’t like your solution because I think that MVVM is more clear, but your way works too.

    I hope this helps you.

    public class Material
    {
        public string Name { get; set; }    
    }
    
    public class ViewModel : INotifyPropertyChanged
    {
        public ViewModel()
        {
            Materials = new Material[] { new Material { Name = "M1" }, new Material { Name = "M2" }, new Material { Name = "M3" } };
        }
    
        private Material[] _materials;
        public Material[] Materials
        {
            get { return _materials; }
            set { _materials = value;
                NotifyPropertyChanged("Materials");
            }
        }
    
        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
    
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
    
            DataContext = new ViewModel();
        }
    
        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            gridtext.DataContext = (lbox.SelectedItem);
        }
    }
    

    .

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
    
        <Grid x:Name="gridtext">
            <TextBlock Text="{Binding Name}" />
        </Grid>
    
        <ListBox x:Name="lbox" 
                 Grid.Row="1"
                 ItemsSource="{Binding Materials}"
                 SelectionChanged="ListBox_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF TextBox, defined like this: <TextBox Text={Binding Path=/Comments} Margin=351,193.91,10,36 x:Name=txtComments IsReadOnly=True
I have the following binding in my wpf application xaml: <TextBox Text={Binding Amount, StringFormat=c}
I have an WPF usercontrol that has a TextBox. I set the text Underline
I have a binding from WPF textbox to a business object. It is a
I have a WPF textBox that is declared as ReadOnly <TextBox IsReadOnly=True IsTabStop=False Width=200
I have a WPF TextBox in which I want to allow autocompleting user names
This other SO question asks about an autocomplete textbox in WPF. Several people have
I have a TextBox in a WPF window bound to a dependency property of
I have a simple text box in a WPF application. I need to know
I have WPF Form which has many buttons with the same code. Appearance of

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.