C# WPF数据绑定:加载x数量;“实例”;在xaml中
我想在xaml中加载x个具有属性的对象实例。 这些实例包含在viewmodel的列表中(我目前有四个)。 我想“生成”尽可能多的xaml,以使用(某种)数据绑定来显示这些实例 注意,我使用的是Caliburn.Micro框架 两个硬编码“实例”的示例C# WPF数据绑定:加载x数量;“实例”;在xaml中,c#,wpf,xaml,C#,Wpf,Xaml,我想在xaml中加载x个具有属性的对象实例。 这些实例包含在viewmodel的列表中(我目前有四个)。 我想“生成”尽可能多的xaml,以使用(某种)数据绑定来显示这些实例 注意,我使用的是Caliburn.Micro框架 两个硬编码“实例”的示例 我的xaml 与之配套的ViewModel class AlleTicketsViewModel:屏幕 { 私有静态只读ITicketManager mgr=new TicketManager(); 私有列表票证=新列表(); 公共AlleT
我的xaml
与之配套的ViewModel
class AlleTicketsViewModel:屏幕
{
私有静态只读ITicketManager mgr=new TicketManager();
私有列表票证=新列表();
公共AlleTicketsViewModel()
{
tickets=mgr.GetTickets().ToList();
}
是否有方法将实例(获取属性)数据绑定到xaml元素(以及当第一个实例被绑定时,选择下一个实例)
xaml可以这样“生成”吗?使用
ItemsControl
并将其ItemsSource
属性绑定到视图模型的collection属性:
<ItemsControl ItemsSource="{Binding Tickets }">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--ticket vette titel-->
<TextBlock Text="Ticket 1" Padding="5 5 5 5" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"
FontWeight="Bold" HorizontalAlignment="Center"/>
<!--linkerkolom 1-->
<StackPanel Margin="0 0 5 0" Grid.Column="0" Grid.Row="1" >
<TextBlock Text="TicketNumber: "/>
<TextBlock Text="Gebruiker: "/>
<TextBlock Text="Datum: "/>
<TextBlock Text="Status: "/>
<TextBlock Text="Vraag/Probleem: "/>
</StackPanel>
<!--rechterkolom 1-->
<StackPanel Grid.Column="1" Grid.Row="1">
<TextBlock Text="{Binding TicketNumber}" />
<TextBlock Text="{Binding Gebruiker} "/>
<TextBlock Text="{Binding Date}"/>
<TextBlock Text="{Binding Status} "/>
<TextBlock Text="{Binding Problem}"/>
</StackPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
您可以使用关联属性创建自定义用户控件,然后实例化它的一个实例并将其添加到父容器中。或者,作为一种更简单的方法,您可以更改listview的项模板并将集合绑定到listview。我更喜欢第二种方法。类似于最后几个示例的方法使用ItemsCon我在谷歌上搜索了一下,需要在代码隐藏文件中初始化itemscontrol。icTodoList.ItemsSource=items;不理想。
class AlleTicketsViewModel : Screen
{
private static readonly ITicketManager mgr = new TicketManager();
private List<Ticket> tickets = new List<Ticket>();
public AlleTicketsViewModel()
{
tickets = mgr.GetTickets().ToList();
}
<ItemsControl ItemsSource="{Binding Tickets }">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--ticket vette titel-->
<TextBlock Text="Ticket 1" Padding="5 5 5 5" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"
FontWeight="Bold" HorizontalAlignment="Center"/>
<!--linkerkolom 1-->
<StackPanel Margin="0 0 5 0" Grid.Column="0" Grid.Row="1" >
<TextBlock Text="TicketNumber: "/>
<TextBlock Text="Gebruiker: "/>
<TextBlock Text="Datum: "/>
<TextBlock Text="Status: "/>
<TextBlock Text="Vraag/Probleem: "/>
</StackPanel>
<!--rechterkolom 1-->
<StackPanel Grid.Column="1" Grid.Row="1">
<TextBlock Text="{Binding TicketNumber}" />
<TextBlock Text="{Binding Gebruiker} "/>
<TextBlock Text="{Binding Date}"/>
<TextBlock Text="{Binding Status} "/>
<TextBlock Text="{Binding Problem}"/>
</StackPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
class AlleTicketsViewModel : Screen
{
private static readonly ITicketManager mgr = new TicketManager();
public List<Ticket> Tickets { get; } new List<Ticket>();
public AlleTicketsViewModel()
{
Tickets = mgr.GetTickets().ToList();
}
}