简体   繁体   中英

How to bind a structure to textbox control in WPF MVVM approach

I wanted to bind three textboxes to three members of a structure. Here is my XAML code:

<TextBox Grid.Column="1" Height="32" HorizontalAlignment="Left" Margin="322,12,0,0" Text="{Binding SelectedStudentDetails.FirstName, Mode=TwoWay}" TextWrapping="Wrap" VerticalAlignment="Top" Width="199" />
<TextBox Grid.Column="1" Height="30" HorizontalAlignment="Left" Margin="322,75,0,0" Text="{Binding SelectedStudentDetails.LastName,Mode=TwoWay}" TextWrapping="Wrap" VerticalAlignment="Top" Width="199" />
<TextBox Grid.Column="1" Height="33" HorizontalAlignment="Left" Margin="322,137,0,0" Text="{Binding SelectedStudentDetails.City,Mode=TwoWay}" TextWrapping="Wrap" VerticalAlignment="Top" Width="199" />

Here is viewModel snippet:

private Student _selectedstudentDetails;
    public Student SelectedStudentDetails
    {
        get {
            return _selectedstudentDetails; 
            }
        set
        {
            if (_selectedstudentDetails != value)
            {
                _selectedstudentDetails = value;
                RaisePropertyChanged("SelectedStudentDetails");

            }
        }
    }




//StudentList is the observable list type

public void AddStudentDetails(object param)
        {
            StudentList.Add(new Student { FirstName = SelectedStudentDetails.FirstName, LastName = SelectedStudentDetails.LastName, City = SelectedStudentDetails.City });
        }

How to populate the populate the Student object using binding structure with textbox controls?

Student class declaration :

namespace SimplestMVVM.Model
{
    public class Student
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string City { get; set; }
    }
}

Well your question is not really clear, but I guess your problem is that SelectedStudentDetails.FirstName, SelectedStudentDetails.LastName and SelectedStudentDetails.City are always null right?

Basically this is because your Student object HAS TO BE a viewModel (or at least INotifyPropertyChanged) has well. Because it has to notify when you update its properties from your view. (And yes it has to be properties). Otherwise it never notifies that you change the values, and they stay null.

The easiest way in my opiniopn would be to create a CLASS StudentViewModel with your required properties. This way your binding will work.

Another solution (but honestly I do not see any good reason to do that, still it should work) could be to directly transform your Student in a INotifyPropertyChanged Class.

Free tip as I'm here, it would be much better in your XAML if you do abuse of margin. Youd could do something like that:

   <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <TextBox Grid.Column="0" Height="30" HorizontalAlignment="Left" Text="{Binding SelectedStudentDetails.FirstName, Mode=TwoWay}" TextWrapping="Wrap" VerticalAlignment="Top" Width="199" />
        <TextBox Grid.Column="1" Height="30" HorizontalAlignment="Left" Text="{Binding SelectedStudentDetails.LastName,Mode=TwoWay}" TextWrapping="Wrap" VerticalAlignment="Top" Width="199" />
        <TextBox Grid.Column="2" Height="30" HorizontalAlignment="Left" Text="{Binding SelectedStudentDetails.City, Mode=TwoWay}" TextWrapping="Wrap" VerticalAlignment="Top" Width="199" />
    </Grid>

为您的学生类实施INotifyPropertyChanged

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM