简体   繁体   English

WPF周日历数据模板

[英]WPF Week Calendar DataTemplate

I want to create a weekly calendar UserControl. 我想创建一个每周日历UserControl。 In the end it should look something like this . 最后应该看起来像这样

I have an ObservableCollection with Events. 我有一个带有事件的ObservableCollection。 Each Event has an start DateTime and an end DateTime. 每个事件都有一个开始日期时间和一个结束日期时间。 I also have a Grid where I would like to place each event: 我还有一个网格,我想在其中放置每个事件:

   <Grid x:Name="week_Grid" Margin="38,0,0,0" ShowGridLines="True">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
</Grid>

Each column represents a week day. 每列代表一个工作日。 The button for each event must have the correct length and offset according to the start/end DateTimes also be in the correct column. 每个事件的按钮必须具有正确的长度和偏移量(根据开始/结束日期时间也位于正确的列中)。

Currently all I managed to do is create a ListBox: 目前,我要做的就是创建一个ListBox:

<ListBox Width="400" Margin="10" ItemsSource="{Binding Path=Events}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Path=Id}" />
                        <TextBlock Text="{Binding Path=Start}" />
                        <TextBlock Text="{Binding Path=End}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

But how can I freely position the elements in the grid, assign the correct GridColumn according to the day of the week and position it correctly at the correct time of the day? 但是,如何才能在网格中自由定位元素,根据星期几分配正确的GridColumn并在一天的正确时间正确定位呢? The Grid does not allow to set an ItemSource. 网格不允许设置ItemSource。

I also have to manage events that start on a different day than they end. 我还必须管理在与结束不同的日期开始的事件。 These events need more then one button. 这些事件需要一个以上的按钮。 Is it even possible to do this with DataTemplates or do I have to manually iterate over the event and create a buttons for each event manually? 甚至可以使用DataTemplates做到这一点,还是必须手动遍历该事件并手动为每个事件创建一个按钮? Which is the best approach? 哪种方法最好?

I decided that it is not possible to do this with just a DataTemplate. 我决定仅使用DataTemplate不可能做到这一点。 I was not able to realise all the necessary logic (eg adding multiple elements for events spanning multiple days). 我无法实现所有必要的逻辑(例如,为跨越多天的事件添加多个元素)。 I instead added an Event Handler in the View to do it manually: 相反,我在视图中添加了一个事件处理程序以手动进行操作:

private void EventsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
       switch (e.Action)
       {
           case NotifyCollectionChangedAction.Add:
              break;
           case NotifyCollectionChangedAction.Replace:
              break;
           case NotifyCollectionChangedAction.Remove:
              break;
           case NotifyCollectionChangedAction.Reset:
              break;
           case NotifyCollectionChangedAction.Move:
              break;
       }
}

And simply passed it to the ViewModel: 并将其简单地传递给ViewModel:

public EventViewModel(System.Collections.Specialized.NotifyCollectionChangedEventHandler changed)
{
    ...
    Events = new ObservableCollection<Event>();
    Events.CollectionChanged += changed;
    ...
}

Taking care of the mentioned problems is then straight forward. 这样就可以轻松解决上述问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM