0

我有一个名为 AllUserViewModel 的视图模型,其中包含一个 UserViewModel 列表。
UserViewModel 与 UserControl 绑定。

这是我的 MainWindow,对于每个用户,我将 UserControl 添加到 stackpanel 容器中。

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

我向每个 UserControlButton 传递一个 UserViewModel

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

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

这是我的 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>

所有用户视图模型

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

和用户视图模型

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

问题是最初显示了 3 个用户,但是当我尝试在鼠标输入事件上更改名称时,尽管触发了 NotifyPropertyChanged,但名称并没有改变。

4

1 回答 1

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

        _user.Name = value;

        NotifyPropertyChanged("UserName");
    }
}

您的属性名为 Name 但您为名为 UserName 的属性引发了 PropertyChanged 事件!

于 2013-09-11T15:09:50.283 回答