Search code examples
c#wpfdata-bindingdatacontext

C# WPF Databinding and Datacontext


I am (new in C# and WPF and) trying to bind data from more sources (class properties) and I am a bit confused from different guides and advices. I want to dynamically add Users in userList and showing for example the last insert and whole list at the same time. That is done on different place in source code, but simple like in contructor of my example. How and where should I set binding and datacontext for those three elements (myName,myTitle and myUserList) to reflect changes in main class properties? Should I call every time function for update binding target, or set this.datacontext after editing properties? Or should I bind to some function (if it's possible) which returns the value I need? I am a bit lost with binding to property of object and datacontexts etc. Here is an example from what I have:

<Window x:Name="Window" x:Class="WpfTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfTest">
  <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="200"/>
        <ColumnDefinition Width="200"/>
    </Grid.ColumnDefinitions>
    <StackPanel>
      <TextBox x:Name="myName" Text=""  Grid.Column="1"/>
      <TextBox x:Name="myTitle" Text=""  Grid.Column="0"/>
    </StackPanel>
    <ListBox x:Name="myUserList">
    </ListBox>
  </Grid>
</Window>

And

public partial class MainWindow : Window {
  public User myUser;
  public Dictionary<int,User> userList= new Dictionary<int, User>();

  public object SubWindow { get; private set; }

  public MainWindow() {
    newUser = new User();
    newUser.Title = "John Doe";
    newUser.Name = "Dr.";
    this.userList.Add(index,newUser);
    this.myUser=newUser;
    InitializeComponent();
  }
}

And

public class User
{
  public String Name { get; set; }
  public String Title { get; set; }
}

Thanks for any advice.


Solution

  • First thing is first, WPF works best when you are working with MVVM, the general idea is implementing the INotifyPropertyChanged, what ever items you add to you change, will propagate to the framework and update your views.

    When working with Lists, use an ObservableCollection. If you want to add items dynamically to it, you would need to modify the ObservableCollection instead. For best results, in your UserControl, use a DataTemplate for the specific type to display a formatted version of your values.

    For the second part, showing the last added item, there are a few ways you can go about this, the best would be to add a new Items(Grid, Stackpanel, etc) that can hold data, use Binding to set its value to a the same context as your list(i.e the ObservableCollection) and create a Converter that will use the ObservableCollection as input, inside your specific converter implementation, just get the last item added and Display it to the control you want( you can use a dataTemplate for this as well)