我有一个用户控件,我想在我的主窗口上加载多次。
为此,我使用了一个ItemsControl
:
<ItemsControl Grid.Row="1"
ItemsSource="{Binding FtpControlList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"
IsItemsHost="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type my:BackUpControl}">
<my:BackUpControl Margin="5"
Width="500" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我的用户控件通过 ViewModel 进行绑定。我的主窗口(MainWindow)也有一个 ViewModel。在 MainWindowViewModel 中,我有一个 Observable Collection 依赖属性,它保存了我的 UserControlViewModel 列表。在 MainWindowViewModel 的构造函数中,我向该列表中添加了一些 UserControlViewModel。
public MainWindowViewModel()
{
FtpControlList = new ObservableCollection<BackUpControlViewModel>();
FtpControlList.Add(new BackUpControlViewModel("View 1"));
FtpControlList.Add(new BackUpControlViewModel("View 2"));
FtpControlList.Add(new BackUpControlViewModel("View 3"));
}
public static readonly DependencyProperty FtpControlListProperty = DependencyProperty.Register("FtpControlList", typeof(ObservableCollection<BackUpControlViewModel>), typeof(MainWindowViewModel));
public ObservableCollection<BackUpControlViewModel> FtpControlList
{
get { return (ObservableCollection<BackUpControlViewModel>)GetValue(FtpControlListProperty); }
set { SetValue(FtpControlListProperty, value); }
}
现在出现了一个问题,它会加载 3 次空的用户控件而不是 FtpControlList 属性中设置为 'View 1, View 2 和 View 3' 的用户控件。我该如何确保从列表中加载 UserControls 而不是空的 UserControls?
UserControlViewModel 的一部分:
// part of the UserControl Viewmodel
public BackUpControlViewModel()
{
}
public BackUpControlViewModel(string header)
{
GroupBoxHeader = header;
}
#region Dependency Properties
public static readonly DependencyProperty GroupBoxHeaderProperty = DependencyProperty.Register("GroupBoxHeader", typeof(string), typeof(BackUpControlViewModel), new UIPropertyMetadata("empty"));
public string GroupBoxHeader
{
get { return (string)GetValue(GroupBoxHeaderProperty); }
set { SetValue(GroupBoxHeaderProperty, value); }
}
public static readonly DependencyProperty FtpUrlProperty = DependencyProperty.Register("FtpUrl", typeof(string), typeof(BackUpControlViewModel), new UIPropertyMetadata("ftpurl"));
public string FtpUrl
{
get { return (string)GetValue(FtpUrlProperty); }
set { SetValue(FtpUrlProperty, value); }
}
public static readonly DependencyProperty FtpUserProperty = DependencyProperty.Register("FtpUser", typeof(string), typeof(BackUpControlViewModel), new UIPropertyMetadata("ftpUser"));
public string FtpUser
{
get { return (string)GetValue(FtpUserProperty); }
set { SetValue(FtpUserProperty, value); }
}
#endregion
可能是我太蠢了,但我似乎找不到它。
MainWindow和UserControl的数据上下文绑定到它的ViewModel。
编辑:BackupControl数据上下文设置为BackupControlViewModel(回答Rachel的问题)
public partial class BackUpControl : UserControl
{
public BackUpControl()
{
InitializeComponent();
this.DataContext = new BackUpControlViewModel();
}
}
BackUpControlViewModel
实际上是一个UserControl
吗?或者你在my:BackUpControl
的UserControl
中设置了DataContext
吗?根据你提供的代码,WPF 应该会加载三个空白的my:BackUpControl
对象,并将这些对象后面的DataContext
绑定到BackUpControlViewModel
。 - Rachel