redux17
redux17

Reputation: 685

WPF TextBox Binding ElementName null?

I have a ListBox control with TypeUsers.When I select some record in Listbox and update Name in TextBox the Name property/textbox return always null. Never take value from TextBox, always null ?

Image description here

This is my code

<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>

EDIT

// 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");
  }
}

Thank you.

Upvotes: -1

Views: 834

Answers (3)

redux17
redux17

Reputation: 685

I have resolved my problem. I have also implemented INotifyPropertyChanged interface in Model class. I'm new in WPF MVVM and I read that this interface is implemented only in the ViewModel for connection with View. Now I have implemented in both classes and everything works great.

Thanks Everyone.

Upvotes: 0

Jonathan Perry
Jonathan Perry

Reputation: 3043

You're binding directly to the WPF control (ListBox) and not the ViewModel. I suggest you add a property in your ViewModel that will bind to the TextBox.Text property, once the data changes or the user had changed the value in the TextBox, then the data will update and be reflected in the UI.

Also, if I remember correctly, at launch, the SelectedItem property of the ListBox is null, so there might be a problem there too, but I'm not certain about that...

Upvotes: 0

AnjumSKhan
AnjumSKhan

Reputation: 9827

Implement INotifyPropertyChanged interface in UserType class.

Upvotes: 0

Related Questions