gpt4 book ai didi

c# - ObservableCollection 中的绑定(bind)用户控件未显示在 uniformgrid 中

转载 作者:太空宇宙 更新时间:2023-11-03 21:03:17 25 4
gpt4 key购买 nike

所以,我对 C#/XAML 还很陌生,一直在尝试通过重新设计一个旧项目来自学 MVVM。我遇到了应该将用户控件添加到 uniformgrid 的问题。如果我自己实现用户控件,它会显示良好,但如果我将它添加到 ObservableCollection,然后尝试将其绑定(bind)到统一网格,the path to the user-control gets displayed rather than the actual UI element .不幸的是,我对 C# 和 MVVM 还很陌生,所以我无法确定具体的问题所在,这使得很难在线搜索。

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

月 View .cs

namespace CMS.Views
{
public partial class MonthView : UserControl
{
public MonthView()
{
InitializeComponent();
MonthViewModel monthViewModelObject = MonthViewModel.GetMonthViewModel();
this.DataContext = monthViewModelObject;
}
}
}

月 View 模型

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

具有 DataTemplate 的 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>

最佳答案

编辑:无论您使用的是像Label 这样的简单控件,同样的规则都适用。或您自己的控件,如 DayView .

您需要设置 ItemsControl.ItemTemplate将绑定(bind)到您的 IReadOnlyCollection<DayViewModel> 中的每个项目.

然后,制作一个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>

你没有显示 DayViewModel类,因此您只需更改 PropertyToDisplay到您希望 View 显示的实际属性。

编辑:制作DayView ItemsControl.Template会自动将其绑定(bind)到 type ItemSource 中的项目.

这意味着你可以治疗 DayViewUserControlDayViewModel作为其 DataContext没有明确设置它。

我假设实际的 View你的DayViewGridDataTemplate里面,所以我只是修改了代码如下:

日 View .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>

关于c# - ObservableCollection 中的绑定(bind)用户控件未显示在 uniformgrid 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43196087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com