1

我有一个与 UserViewModel 绑定的 UserControl

这是我简化的 UserControlButton

<UserControl
  x:Class="UserControlSolution.UserControlButton"
  x:Name="UserControl"
>

<Grid x:Name="LayoutRoot" Height="50" RenderTransformOrigin="0.5,0.5" VerticalAlignment="Top" HorizontalAlignment="Stretch">  

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

</Grid>
</UserControl>

这是视图模型

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

我还有一个存储所有 UserViewModel 的 ViewModel

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

然后在主窗口中填写 AllUserViewModel (用于测试)

 public AllUserViewModel allUserViewModel { get; set; }

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

        allUserViewModel = new AllUserViewModel();

        Robby = new User(1, "Robby", "Calling");
        Kevin = new User(2, "Kevin", "Calling");
        Laurens = new User(3, "Laurens", "Calling");
        Erwin = new User(4, "Erwin", "Calling");

        allUserViewModel.AddUser(Robby);
        allUserViewModel.AddUser(Kevin);
        allUserViewModel.AddUser(Laurens);
        allUserViewModel.AddUser(Erwin);

        this.DataContext = allUserViewModel;
    }

现在的问题是,如何为 AllUserViewModel 集合中的每个 UserViewModel 添加一个 UserControlButton ?

4

1 回答 1

2

为了在列表框中显示用户控件,您只需要为列表框定义 ItemTemplate,例如

  <ListBox ItemsSource="{Binding Users}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <local:UserControl1/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
于 2013-09-11T13:36:29.303 回答