WPF TextBox绑定ElementName null?

时间:2016-01-26 10:28:51

标签: wpf binding elementname

我有一个带有TypeUsers的ListBox控件。当我在Listbox中选择一些记录并更新TextBox中的Name时,Name属性/ textbox将返回null。永远不要从TextBox获取值,总是null?

Image description here

这是我的代码

<ListBox x:Name="LstTypeUsers"
             Grid.Row="0" Grid.Column="4"
             Width="220" Height="120"
             ScrollViewer.VerticalScrollBarVisibility="Visible"
             ItemsSource="{Binding TypeUsers}"
             DisplayMemberPath="Name">
</ListBox>

 <TextBox 
             Grid.Row="0" Grid.Column="2"
             x:Name="txtName"
             HorizontalAlignment="Left" Height="23" 
             TextWrapping="Wrap" 
             VerticalAlignment="Top" Width="170" 
             Text="{Binding ElementName=LstTypeUsers, Path=SelectedItem.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
             Validation.ErrorTemplate="{x:Null}"/>

<Button 
            Grid.Column="0"
            HorizontalAlignment="Left" 
            VerticalAlignment="Top" 
            Width="100" Height="30"
            Command="{Binding UpdateTypeUserCmd}" 
            Grid.ColumnSpan="3" Margin="20,90,0,0">
            <StackPanel Orientation="Horizontal">
                <Image Source="/Images/Save.png" />
                <TextBlock Width="55" Height="18" ><Run Text="   "/><Run Text="Update"/></TextBlock>
            </StackPanel>
        </Button>

修改

// Model class
public class UserType: INotifyPropertyChanged
{
    [Key]
    private int usertypeId;
    public int UserTypeId 
    { 
     get 
     {
         return this.usertypeId;
     }
     set
     {
         this.usertypeId = value;
         OnPropertyChanged("UserTypeId");
     }
    }

    [MaxLength(200)]
    private string name;
    public string Name
    { 
     get 
     {
         return this.name;
     }
     set
     {
         this.name = value;
         OnPropertyChanged("Name");
     }
    }

    [Required]
    private bool status;
    public bool Status
    { 
     get 
     {
         return this.status;
     }
     set
     {
         this.status = value;
         OnPropertyChanged("Status");
     }
    } 

    public virtual ObservableCollection<User> User { get;   private set; }

    public UserType()
    {
        this.User = new ObservableCollection<User>();

    }
}

 // ViewModelBase class
 public event PropertyChangedEventHandler PropertyChanged;
 public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

// UserTypeViewModel

public class UserTypeViewModel

private UserType _userType;
private ObservableCollection<UserType> _UserTypeList;

// Constructor
public UserTypeViewModel()
{
   _userType = new UserType();
   _UserTypeList = new ObservableCollection<UserType>(GetUserTypeAll());
}

public ObservableCollection<TypeUsers> TypeUsers
{
  get
  {
     return _UserTypeList;
  }
  set
  {
     _UserTypeList = value;
     //OnPropertyChanged("TypeUsers");
  }
}

public string Name
{
  get
  {
     return _userType.Name;
  }
  set
  {
      _userType.Name = value;
      //OnPropertyChanged("Name");
  }
}

谢谢。

3 个答案:

答案 0 :(得分:0)

INotifyPropertyChanged课程中实施UserType界面。

答案 1 :(得分:0)

您直接绑定到WPF控件(ListBox)而不是ViewModel。我建议您在ViewModel中添加一个属性,该属性将绑定到TextBox.Text属性,一旦数据发生更改或用户更改了TextBox中的值,则数据将更新并反映在UI。

另外,如果我没记错的话,在发布时,SelectedItem的{​​{1}}属性为空,所以也可能存在问题,但我不确定...

答案 2 :(得分:0)

我已经解决了我的问题。我还在Model类中实现了INotifyPropertyChanged接口。我是WPF MVVM的新手,我读到这个界面只在ViewModel中实现,用于与View连接。现在我已经在两个类中实现了,一切都很好。

谢谢大家。