Question

I have a WPF forms and a class Users (content attributes Id, Login and Name), in my class of this forms, I had get a Users object for put this information in the form with DataContext and Binding

I can put this Users object to my Window.DataContext (this.DataContext = usersObject;) with code behind, but I think if I can make this with XAML, maybe is better

I have set a attribute in my class UserForm (public Users usersObject {get; set;})

My form UserForm : Window

<Window DataContext="{What need I put here?">
    <Grid>
        <TextBlock Text="Id:"/>
        <TextBox Name="Id" Text="{Binding Path=Id}"/>

        <TextBlock Text="Login:"/>
        <TextBox Name="Login" Text="{Binding Path=Login}"/>

        <TextBlock Text="Name:"/>
        <TextBox Name="Name" Text="{Binding Path=Name}"/>
    </Grid>
</Window>

UserForm.xaml.cs

public class UserForm : Window
{
    public Users userObject { get; set; }

    public UserForm(Users user)
    {
        InitializeComponent();
        this.userObject = user;
    }
}

My class Users

public class Users
{
    public int Id { get; set; }
    public string Login { get; set; }
    public string Name { get; set; }
}

How to I do for set userObject in the itself Window.DataContext for TextBox's can put it values?

Was it helpful?

Solution

Some time back I had written an article on different options of binding XAML control to code behind property. This might help you.

http://codingseason.blogspot.in/2013/05/data-binding-xaml-control-to-property.html

OTHER TIPS

Remove the DataContext binding since you are not explicitly doing MVVM pattern. No point of doing the binding.

In your Window.xaml

<Window>
<Grid>
    <TextBlock Text="Id:"/>
    <TextBox Name="Id" Text="{Binding Path=Id}"/>

    <TextBlock Text="Login:"/>
    <TextBox Name="Login" Text="{Binding Path=Login}"/>

    <TextBlock Text="Name:"/>
    <TextBox Name="Name" Text="{Binding Path=Name}"/>
</Grid>

Add this to your Behind the code Window.xaml.cs

    public class UserForm : Window
    {
        public Users userObject { get; set; }

        public UserForm(Users user)
        {
            InitializeComponent();
            this.DataContext = user;
        }
    }

If you would do it in MVVM you would have done something like this

ViewModel.cs

    public class UserViewModel
    {
        private Users _model;
        public UserViewModel(Users model)
        {
            _model = model;
        }

        public int Id { get { return _model.Id; } }
        public string Login { get { return _model.Login; } set { _model.Login; } }
        public string Name { get { return _model.Name; } set { _model.Name; } }

    }

Now the ViewModel can be customize depending on what you need, you can expose the Model, inject it to the constructor or just set the property value if it is exposed. Don't forget to implement INotifyPropertyChanged if you want to propagate any values in the ViewModel back to the user interface.

View.xaml.cs

public class UserForm : Window {

    public UserForm(Users user)
    {
        InitializeComponent();
        this.DataContext = new UserViewModel(user);
    }

View.xaml

You have two choices, you can explicitly set the DataContext just like what I did behind the code or you can create a public property that returns the UserViewModel. It's just the same

Model.cs

public class Users { //Whatever properties you need }

Now this is a very simplistic example of MVVM pattern. Once you know the basics, you can then integrate some helpful frameworks that implements MVVM for you like Caliburn Micro and MVVM Toolkit

Create an object of UserForm and assigning data context is pretty simple.

UserForm userFormView    = new UserForm ();
Users dataContextObject  = new Users();
userFormView.DataContext = dataContextObject;
userFormView.Show()          //you can also use showdialog. Thats up to you

Its better you remove the following code:

public Users userObject { get; set; }

public UserForm(Users user)
{
    InitializeComponent();
    this.userObject = user;
}

Its better to separate the view and the viewmodel. Have a look at MVVM and it will make more sense

This is one of the way to assign DataContext directly from xaml.

Example App.xaml

<Application.Resources>
     <local:Users x:Key="Users"/>    
</Application.Resources>

Example UserForm.xaml

<Window DataContext="{StaticResource Users}">
    <Grid>
        <TextBlock Text="Id:"/>
        <TextBox Name="Id" Text="{Binding Path=Id}"/>

        <TextBlock Text="Login:"/>
        <TextBox Name="Login" Text="{Binding Path=Login}"/>

        <TextBlock Text="Name:"/>
        <TextBox Name="Name" Text="{Binding Path=Name}"/>
    </Grid>
</Window>

Maybe you can define a DependencyProperty to wrap the access to the userObject, then bind it to the DataContext

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top