Click here to Skip to main content
15,895,192 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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 ?

This is my code
XAML
<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>


C#
// 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: ViewModelBase

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


C#
public ICommand UpdateTypeUserCmd
        {
            get { return new RelayCommand(k => UpdateTypeUser(Name, Status)); }
        }

        private void UpdateTypeUser(string name, bool status)
        {
            if (!IsStringMissing(name))
            {
                using (var typeUserManager = new TipUserManager())
                {
                    var typeUser = new TypeUser
                    {
                        TypeUserId = TypeUserId,
                        Name = name,
                        Status = status,
                    };

                    if (typeUserManager.Update(typeUser))
                    {
                        MessageBox.Show("Sucessfully!", "OK", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                    }
                }
            }
            else
                MessageBox.Show(Required fields!", "Warning!", MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }
Posted
Updated 2-Feb-16 9:26am
v6
Comments
Sergey Alexandrovich Kryukov 2-Feb-16 10:51am    
In what line do you get this null?
—SA
dev201 2-Feb-16 12:38pm    
I'm developing in WPF-MVVM and getting null in Name property for UserType class.
Sergey Alexandrovich Kryukov 2-Feb-16 13:12pm    
Sorry for my typo. I wanted to ask: in what line?
—SA
dev201 2-Feb-16 13:56pm    
In my TextBox property Name is null when I try to save/update changes from this texbox.

Text="{Binding ElementName=LstTypeUsers, Path=SelectedItem.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"

Sergey Alexandrovich Kryukov 2-Feb-16 14:35pm    
Is it the XAML fragment? But how do you know that Name is null? It should, say, throw exception in some code. Do you mean SelectedItem.Name?
But what if nothing is selected? Are you sure you want to use Name, nothing else? It sounds weird. What is that property? A link?
—SA

1 solution

I have resolved my problem. I have also implemented INotifyPropertyChanged interface in Model class. I am 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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900