简体   繁体   English

如何在WPF MVVM方法中将结构绑定到文本框控件

[英]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: 这是我的XAML代码:

<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: 这是viewModel片段:

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对象?

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? 好吧,您的问题不是很清楚,但是我想您的问题是SelectedStudentDetails.FirstName,SelectedStudentDetails.LastName和SelectedStudentDetails.City始终为空,对吗?

Basically this is because your Student object HAS TO BE a viewModel (or at least INotifyPropertyChanged) has well. 基本上这是因为您的Student对象必须是viewModel(或者至少是INotifyPropertyChanged)。 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. 在我的观点中,最简单的方法是使用所需属性创建CLASS StudentViewModel。 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. 另一个解决方案(但老实说,我看不出有什么充分的理由这样做,但仍然应该可行)可以是直接将您的Student转换为INotifyPropertyChanged类。

Free tip as I'm here, it would be much better in your XAML if you do abuse of margin. 就像我在这里的免费提示,如果您滥用保证金,那么在您的XAML中会更好。 Youd could do something like that: Youd可以做这样的事情:

   <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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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