【问题标题】:Add Usercontrol as ListBoxItem from ViewModel从 ViewModel 添加 Usercontrol 作为 ListBoxItem
【发布时间】:2015-01-15 19:34:54
【问题描述】:

我对 MVVM(和 WPF)非常陌生。我有一个主视图,其中有一个列表框,其中添加了 x 次相同的用户控件。添加这些控件没问题。 UC 包含一个 ListBox,它将包含未知数量的项目。这些项目可以是另一个 UC 吗?如何不破坏 MVVM 并将项目添加到每个添加的 UC 的 ListBox 中?

有人能指出正确的方向吗?

【问题讨论】:

    标签: wpf mvvm


    【解决方案1】:

    您可以在列表框的 ItemTemplate 中的 Datatemplate 中添加用户控件。请参考以下示例。

    <Window x:Class="MSDN15Jan2015_Learning.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MSDN15Jan2015_Learning"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox ItemsSource="{Binding PersonList}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <local:ListBoxItemControl/> 
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
    

     public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainViewModel();
        }
    }
     class MainViewModel
    {
        private ObservableCollection<Person> perList = new ObservableCollection<Person>();
    
        public ObservableCollection<Person> PersonList 
        {
            get { return perList; }
            set { perList = value; }
        }
    
    
        public MainViewModel()
        {
            perList.Add(new Person() { Age = 1, Name = "Test1"});
            perList.Add(new Person() { Age = 2, Name = "Test2" });
            perList.Add(new Person() { Age = 3, Name = "Test3" });
            perList.Add(new Person() { Age = 4, Name = "Test4" });
        }
    }
    
    public class Person
    {
        private int age;
    
        public int Age
        {
            get { return age; }
            set { age = value; }
        }
    
        private string name;
    
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }
    <UserControl x:Class="MSDN15Jan2015_Learning.ListBoxItemControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <StackPanel>
            <TextBlock Text="{Binding Name}"/>
            <TextBlock Text="{Binding Age}"/>
        </StackPanel>
    </Grid>
    

    【讨论】:

    • 谢谢!这让我发疯了。
    • 你得到这个答案了吗..你需要任何解释吗?
    • 全部设置 - 工作正常。
    猜你喜欢
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多