【问题标题】:DataGrid - a clean way to display unrelated cell values in a row?DataGrid - 一种在行中显示不相关单元格值的简洁方法?
【发布时间】:2014-01-28 19:37:45
【问题描述】:

我是 WPF 新手,我会尽量保持简短。

我想要实现的是显示一个表格,其中单元格值彼此不相关,每个代表一个单独的对象。

更准确地说,我需要一个类似于日历的视图,其中包含每天的相关任务,如下所示:

显示的天数是可变的。
重要的是按时间顺序列出日期,并保留特定日期的任务顺序(很像列表框列表)。

那么我该如何实现呢?

DataGrid 似乎只将行绑定到数据。我想实现一个具有可变数量的 DependencyProperties 的适配器列表,这些适配器会伪装成行。但是对于这么简单的表格来说,这似乎有点过于复杂了。

我还研究了如何使 DataGrid 水平,但它更多的是额外的代码。

感谢任何帮助。干杯:)

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    可以每天只使用ListBox,或者甚至只使用ItemsControl,并且拥有任意数量的它们......您只需要正确地构造您的数据。假设您有一个 Day 类,其中包含一个 Date 和一个名为 Tasks 的集合:

    public class Day // Implement INotifyPropertyChanged correctly here
    {
        public DateTime Day { get; set; }
        public ObservableCollection<string> Tasks { get; set; }
    }
    

    现在在您的视图模型中,您只需要Day 实例的集合:

    public ObservableCollection<Day> Days { get; set; }
    

    那么你只需要一个DataTemplate 来为每个Day 实例定义你的ListBox

    <DataTemplate DataType="{x:Type DataTypes:Day}">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" Text="{Binding Date, StringFormat={}{0:MMM d}}" />
            <ListBox Grid.Row="1" ItemsSource="{Binding Tasks}" />
        </Grid>
    </DataTemplate>
    

    最后,添加ListBoxItemsControl 以显示Days 的集合并将ItemsPanel 设置为StackPanel,并将其Orientation 属性设置为Horizontal

    <ItemsControl DockPanel.Dock="Top" ItemsSource="{Binding Days}" Name="overlayItems">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
    

    添加一些测试数据和你的离开:

    Days = new ObservableCollection<Day>();
    Days.Add(new Day() { Date = new DateTime(2014, 5, 1), Tasks = new ObservableCollection<string>() { "Doing something today", "Doing something else today" } });
    Days.Add(new Day() { Date = new DateTime(2014, 5, 2), Tasks = new ObservableCollection<string>() { "Doing nothing today" } });
    Days.Add(new Day() { Date = new DateTime(2014, 5, 3), Tasks = new ObservableCollection<string>() { "Doing something today" } });
    

    我会把更详细的信息留给你。

    【讨论】:

    • 由于缺乏更简单的解决方案,我会选择这个。谢谢:)
    【解决方案2】:

    假设您有一个名为AppointmentViewModel 的视图模型对象,其中包含计划在当天完成的所有任务。然后,您可以为名为@9​​87654322@ 的一行创建一个视图模型类,例如。然后每天可能有一个属性:

    public class WeekViewModel : ViewModelBase {
    
        public AppointmentViewModel Sunday { get { . . . } set { . . . } }
    
        public AppointmentViewModel Monday { get { . . . } set { . . . } }
    
       // and so on for the rest of the week
    }
    

    然后,您将拥有一个带有DataGrid 控件的表单的视图模型对象。这将有一个类型为WeekViewModel 的属性。让我们调用集合属性Appointments

    DataGrid 的 XAML 如下所示:

    <DataGrid AutoGenerateColumns="False"
              ItemsSource="{Binding Path=Appointments}"
              Name="ThisMonthsAppointmentsGrid">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=Sunday}" />
            <DataGridTextColumn Binding="{Binding Path=Monday}" />
            <!-- Other DataGridTextColumn definitions for other days of the week -->
        </DataGrid.Columns>
    </DataGrid>
    

    【讨论】:

    • 这就是我所说的“适配器列表”。不幸的是,显示的天数是可变的,所以我必须动态注册 DependencyProperties。不过感谢您的宝贵时间
    猜你喜欢
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 2012-01-20
    • 2021-01-11
    • 2021-11-06
    • 1970-01-01
    • 2011-05-27
    相关资源
    最近更新 更多