0

I have a viewmodel named AllUserViewModel that contains a list of UserViewModels.
The UserViewModel is binded with a UserControl.

This is my MainWindow, foreach user I add a UserControl to a stackpanel container.

public partial class MainWindow : Window
{
    public AllUserViewModel allUserViewModel { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        DwmDropShadow.DropShadowToWindow(this);

        allUserViewModel = new AllUserViewModel();
        this.DataContext = allUserViewModel;

        allUserViewModel.Users.Add(new UserViewModel(new User(1, "Robby", "Bezet")));
        allUserViewModel.Users.Add(new UserViewModel(new User(2, "Erwin", "Bezet")));
        allUserViewModel.Users.Add(new UserViewModel(new User(3, "Kevin", "Bezet")));

        foreach (UserViewModel u in allUserViewModel.Users)
        {
            Container.Children.Add(new UserControlButton(u));
        }

        Container.MouseEnter += new MouseEventHandler(Container_MouseEnter);
    }

    void Container_MouseEnter(object sender, MouseEventArgs e)
    {
        UserViewModel u = allUserViewModel.GetUser(2);
        u.Name = "Laurens";   // Doesn't work
    }
}

To each UserControlButton I pass a UserViewModel

public partial class UserControlButton : UserControl
{
    public UserViewModel userViewModel { get; set; }

    public UserControlButton(UserViewModel u)
    {
        this.InitializeComponent();
        this.DataContext = u;
    }
}

And this is my UserControlButton

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:ee="http://schemas.microsoft.com/expression/2010/effects"
mc:Ignorable="d"
x:Class="UserControlSolution.UserControlButton"
x:Name="UserControl"
Height="50" Background="#FF2F2F2F"
VerticalAlignment = "Top"
Margin="2,0,0,5"
>

    <StackPanel x:Name="UserContainer" Orientation="Vertical" VerticalAlignment="Center" Grid.Column="1" Background="{DynamicResource DarkGrey}">
        <TextBlock x:Name="NameLabel" FontSize="16" Foreground="#FFE5E5E5" Text="{Binding Name}" VerticalAlignment="Top" FontFamily="Segoe UI Semibold" RenderTransformOrigin="0.5,0.5" Margin="0,0,0,2"/>
        <TextBlock x:Name="UserStatusLabel" Text="{Binding UserStatus}" TextWrapping="NoWrap" VerticalAlignment="Top" Foreground="#FFE5E5E5" />
    </StackPanel>

</UserControl>

AllUserViewmodel

public class AllUserViewModel : BaseViewModel
{
    ObservableCollection<UserViewModel> _users;

    public AllUserViewModel()
    {
        Users = new ObservableCollection<UserViewModel>();
    }

    /// <summary>
    /// Observable Collection of Users
    /// Uses INotifyPropertyChange when list changes
    /// </summary>
    public ObservableCollection<UserViewModel> Users
    {
        get { return _users; }
        set
        {
            if (_users != value)
            {
                _users = value;
                NotifyPropertyChanged("Users");
            }
        }
    }

    public void AddUser(User user)
    {
        UserViewModel userViewModel = new UserViewModel(user);
        Users.Add(userViewModel);
    }

    public UserViewModel GetUser(int ID)
    {
        foreach (UserViewModel u in Users)
        {
            if(u.ID == ID)
                return u;
        }

        return null;
    }
}

And UserViewModel

public class UserViewModel : BaseViewModel
{
    readonly User _user;

    public UserViewModel(User user)
    {
        if (user == null)
            throw new ArgumentNullException("User");

        _user = user;
    }

    public string Name
    {
        get { return _user.Name; }
        set
        {
            if (value == _user.Name)
                return;

            _user.Name = value;

            NotifyPropertyChanged("UserName");
        }
    }

    public string UserStatus
    {
        get { return _user.UserStatus; }
        set
        {
            if (value == _user.UserStatus)
                return;

            _user.UserStatus = value;

            NotifyPropertyChanged("UserStatus");
        }
    }

    public int ID
    {
        get { return _user.ID; }
    }
}

The problem is that the 3 users are shown initially, but when I try to change the name on the mouse enter event, the name is not changed although the NotifyPropertyChanged was triggered.

2
  • 2
    Dude, remove all that immediately, and use an ItemsControl. Don't create or manipulate UI elements in procedural code in WPF. Commented Sep 11, 2013 at 15:08
  • I haven't heard of ItemsControl, I'll check it out. Commented Sep 11, 2013 at 15:15

1 Answer 1

2
public string Name
{
    get { return _user.Name; }
    set
    {
        if (value == _user.Name)
            return;

        _user.Name = value;

        NotifyPropertyChanged("UserName");
    }
}

Your property is called Name but you raise a PropertyChanged event for a property called UserName !

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.