C# ObservableCollection中绑定的用户控件不是';t出现在uniformgrid中
所以,我对C#/XAML还相当陌生,一直在尝试通过修改一个旧项目来自学MVVM。我遇到了一个问题,用户控件应该添加到uniformgrid中。如果我自己实现用户控件,它会显示得很好,但是如果我将其添加到ObservableCollection,然后尝试将其绑定到uniformgrid。不幸的是,我对C#和MVVM还不够了解,我无法确定具体是什么问题,这使得在线搜索很困难C# ObservableCollection中绑定的用户控件不是';t出现在uniformgrid中,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,所以,我对C#/XAML还相当陌生,一直在尝试通过修改一个旧项目来自学MVVM。我遇到了一个问题,用户控件应该添加到uniformgrid中。如果我自己实现用户控件,它会显示得很好,但是如果我将其添加到ObservableCollection,然后尝试将其绑定到uniformgrid。不幸的是,我对C#和MVVM还不够了解,我无法确定具体是什么问题,这使得在线搜索很困难 <UserControl x:Class="CMS.Views.MonthView" xmln
<UserControl x:Class="CMS.Views.MonthView"
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"
xmlns:local="clr-namespace:CMS.Views"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<ItemsControl ItemsSource="{Binding Dates}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid IsItemsHost="True" Columns="7"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</UserControl>
MonthViewModel
namespace CMS.ViewModels
{
class MonthViewModel
{
private readonly ObservableCollection<DayViewModel> _dates = new ObservableCollection<DayViewModel>();
public IReadOnlyCollection<DayViewModel> Dates
{
get { return _dates; }
}
public static MonthViewModel GetMonthViewModel()
{
var month = new MonthViewModel();
month.testdaymodel();
return month;
}
public void testdaymodel()
{
DayViewModel DVM = DayViewModel.GetDayViewModel();
DVM.LoadDate(DateTime.Now);
_dates.Add(DVM);
}
}
}
namespace CMS.ViewModels
{
类MonthViewModel
{
私有只读ObservableCollection _dates=新ObservableCollection();
公共IReadOnlyCollection日期
{
获取{返回日期;}
}
公共静态MonthViewModel GetMonthViewModel()
{
var month=new MonthViewModel();
month.testdaymodel();
返回月份;
}
公共void testdaymodel()
{
DayViewModel DVM=DayViewModel.GetDayViewModel();
DVM.LoadDate(DateTime.Now);
_添加日期(DVM);
}
}
}
DayView的XAML,其中包含数据模板
<UserControl x:Class="CMS.Views.DayView"
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"
xmlns:local="clr-namespace:CMS.Views"
mc:Ignorable = "d"
MinWidth="100" MinHeight="100" BorderBrush="LightSlateGray" BorderThickness="0.5,0.5,1.5,1.5">
<UserControl.Resources>
<ResourceDictionary>
</ResourceDictionary>
</UserControl.Resources>
<DataTemplate x:Name ="DayBox">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="21"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border x:Name="DayLabelRowBorder" CornerRadius="2" Grid.Row="0" BorderBrush="{x:Null}" Background="{DynamicResource BlueGradientBrush}">
<Label x:Name="DayLabel" Content="{Binding Path = Info.Day, Mode = OneWay}" FontWeight="Bold" FontFamily="Arial"/>
</Border>
<!--This will be bound to the event schedule for a given day-->
<StackPanel Grid.Row="1" x:Name="DayAppointmentsStack" HorizontalAlignment="Stretch" Background="White" VerticalAlignment="Stretch">
</StackPanel>
</Grid>
</DataTemplate>
</UserControl>
编辑:无论您使用的是像
标签这样的简单控件,还是您自己的控件像DayView
,都适用相同的规则
您需要设置ItemsControl.itemstemplate
,它将绑定到IReadOnlyCollection
中的每个项目
然后,根据您的喜好制作一个DataTemplate
。像这样:
<ItemsControl ItemsSource="{Binding Dates}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid IsItemsHost="True" Columns="7"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- This control is automatically bound to each DayViewModel instance -->
<local:DayView />
<!--
<Label Content="{Binding PropertyToDisplay}" ></Label>
-->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
编辑:无论您使用的是像标签这样的简单控件,还是像日视图这样的自己的控件,都适用相同的规则
您需要设置ItemsControl.itemstemplate
,它将绑定到IReadOnlyCollection
中的每个项目
然后,根据您的喜好制作一个DataTemplate
。像这样:
<ItemsControl ItemsSource="{Binding Dates}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid IsItemsHost="True" Columns="7"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- This control is automatically bound to each DayViewModel instance -->
<local:DayView />
<!--
<Label Content="{Binding PropertyToDisplay}" ></Label>
-->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
“我不想把你们都淹没在无关的代码中”——这里也没有人希望你们这么做。但这并不意味着您可以发布不完整的代码并期望得到答案。你的工作是写一篇可靠地再现你的问题的文章,并提供你在解决问题方面所做的努力的必要细节,以及你遇到的具体问题。另请参见,以及该页面底部链接的文章。同时:“如果我单独实现用户控件,它会显示良好,但是如果我将其添加到ObservableCollection中”——这是否意味着您实际上得到了一个ObservableCollection
对象?如果是这样,那就错了。你的集合应该是视图模型,你应该有一个使用你想要的视图对象的视图模型类型的DataTemplate
(在这种情况下,这个视图对象听起来像是UserControl
,但是如果没有一个好的,这就是我链接Git存储库的具体原因,这样,如果我遗漏了什么,人们可以查看任何其他代码。Peter,你可以回头看看问题,我添加了DayView XAML。我不知道有必要问这个问题。在其中添加了DataTemplate。“这就是我链接Git存储库的具体原因”——使用外部源支持您的问题对问题有害,特别是针对堆栈溢出标准。作为一个政策问题,许多人(包括我自己)不会去看这些来源。MCVE的目的是确保人们不必查看过多的代码,保留问题中的所有代码的目的是确保问题在堆栈溢出时始终有效,即使删除了外部源。每个堆栈溢出问题都需要是独立的。“我不想让你们都淹没在不相关的代码中”——这里也没有人希望你们这样做。但这并不意味着您可以发布不完整的代码并期望得到答案。你的工作是写一篇可靠地再现你的问题的文章,并提供你在解决问题方面所做的努力的必要细节,以及你遇到的具体问题。另请参见,以及该页面底部链接的文章。同时:“如果我单独实现用户控件,它会显示良好,但是如果我将其添加到ObservableCollection中”——这是否意味着您实际上得到了一个ObservableCollection
对象?如果是这样,那就错了。你的集合应该是视图模型,你应该有一个使用你想要的视图对象的视图模型类型的DataTemplate
(在这种情况下,这个视图对象听起来像是UserControl
,但是如果没有一个好的,这就是我链接Git存储库的具体原因,这样,如果我遗漏了什么,人们可以查看任何其他代码。Peter,你可以回头看看问题,我添加了DayView XAML。我不知道有必要问这个问题。在其中添加了DataTemplate。“这就是我链接Git存储库的具体原因”——使用外部源支持您的问题对问题有害,特别是针对堆栈溢出标准。作为一个政策问题,许多人(包括我自己)不会去看这些来源。MCVE的目的是确保人们不必查看过多的代码,保留问题中的所有代码的目的是确保问题在堆栈溢出时始终有效,即使删除了外部源。每个堆栈溢出问题都需要是自包含的。所以,我对datatemplate有点困惑。我显示的不是属性,而是整个用户控件。比如,我试图创建一个一天中整个视图的网格,而不是一个属性。
<UserControl x:Class="CMS.Views.DayView"
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"
xmlns:local="clr-namespace:CMS.Views"
mc:Ignorable = "d"
MinWidth="100" MinHeight="100" BorderBrush="LightSlateGray" BorderThickness="0.5,0.5,1.5,1.5">
<UserControl.Resources>
<ResourceDictionary>
</ResourceDictionary>
</UserControl.Resources>
<!-- <DataTemplate x:Name ="DayBox"> -->
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="21"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border x:Name="DayLabelRowBorder" CornerRadius="2" Grid.Row="0" BorderBrush="{x:Null}" Background="{DynamicResource BlueGradientBrush}">
<Label x:Name="DayLabel" Content="{Binding Path = Info.Day, Mode = OneWay}" FontWeight="Bold" FontFamily="Arial"/>
</Border>
<!--This will be bound to the event schedule for a given day-->
<StackPanel Grid.Row="1" x:Name="DayAppointmentsStack" HorizontalAlignment="Stretch" Background="White" VerticalAlignment="Stretch">
</StackPanel>
</Grid>
<!-- </DataTemplate> -->
</UserControl>