将模型值绑定到datagrid [英] Bind model values to datagrid

查看:84
本文介绍了将模型值绑定到datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,我有一个模型(基本上在视图模型中它将是模型列表)

I have a small question, I have a model (well basically in view model it will be list of models)

public class FlagAnalysis
{
    public DateTime DailyTimestampInterval { get; set; } = DateTime.UtcNow;

    public int[] Flags { get; set; } = {};
}

在视图模型中:

public ObservableCollection<FlagAnalysis> FlagValues { get; set; } = new ObservableCollection<FlagAnalysis>()

因为你看到我有一个带有Datetime和int集合的模型集合,现在我想基于此构建一个datagrig,所以它看起来像:

So as you see I have a collection of model with a Datetime and a collection of int, Now i want to build a datagrig based on this so it would look like:

  02.03.2018 | 03.03.2018 | 04.03.2018
  --------------------------------------
       1     |      0     |     3
       2     |      2     |     1

对于每个DailyTimestampInterval作为标题标题,我想要一个包含标志集合值的collumn ,

For every DailyTimestampInterval as a header title, i want a collumn that contains flags collection value,

现在我的xaml看起来像

for now my xaml looks like

<DataGrid ItemsSource="{Binding FlagValues}" AutoGenerateColumns="False" >

< DataGrid.Resources > < businessLogic BindingProxy   x = &qu ot;代理"  数据 =" { 绑定 } "   />

<DataGrid.Resources> <businessLogic:BindingProxy x:Key="proxy" Data="{Binding}" />

< / DataGrid.Resources > < DataGrid.Columns >

< DataGridTemplateColumn  标题 =" { 装订  数据 时间戳  来源 = { StaticResource   proxy }} "   />

<DataGridTemplateColumn Header="{Binding Data.Timestamps, Source={StaticResource proxy}}" />

< / DataGrid.Columns < span style ="color:blue">>

< / DataGrid >

sutch是否可能










推荐答案

嗨  WojszaM,

Hi  WojszaM,

也就是说,假设有多个项目在FlagAnalysis中,此数据网格中有多少列?

这不是DataGrid应该做的。

That is not DataGrid should do .

< span class ="x_x_short_text"id ="x_x_result_box"lang ="en"tabindex =" - 1"> 改用ListBox或ItemControl,覆盖
ItemsPanel 进行安排这几项。定义datatemplate以显示Item。

Use ListBox or ItemControl instead, override the ItemsPanel to arrange the items. Define the datatemplate to display Item.

    <ListBox
                    Height="300"
                    Margin="10"
                    ItemsSource="{Binding observableCollection, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <VirtualizingStackPanel Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid >
                                <Grid.RowDefinitions>
                                    <RowDefinition  />
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <Border BorderThickness="0,0,0,1">
                                    <Border.BorderBrush>
                                        <DrawingBrush Viewport="0,0,8,8" ViewportUnits="Absolute" TileMode="Tile">
                                            <DrawingBrush.Drawing>
                                                <DrawingGroup>
                                                    <GeometryDrawing Brush="Black">
                                                        <GeometryDrawing.Geometry>
                                                            <GeometryGroup>
                                                                <RectangleGeometry Rect="0,0,50,50" />
                                                                <RectangleGeometry Rect="50,50,50,50" />
                                                            </GeometryGroup>
                                                        </GeometryDrawing.Geometry>
                                                    </GeometryDrawing>
                                                </DrawingGroup>
                                            </DrawingBrush.Drawing>
                                        </DrawingBrush>
                                    </Border.BorderBrush>
                                    <TextBlock  Margin="0,0,5,5" Text="{Binding Path=DailyTimestampInterval}" />
                                </Border>
                        
                              
                                <ItemsControl HorizontalAlignment="Center" Grid.Row="1" ItemsSource="{Binding Path=Flags}"/>


                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>




 

最诚挚的问候,


 

Best Regards,

Bob


这篇关于将模型值绑定到datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆