在 WPF 中制定自定义计划 [英] Making a customized schedule in WPF

查看:26
本文介绍了在 WPF 中制定自定义计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 vs10 的新手,我正在尝试使用 WPF 制作自定义日程表/日历.到目前为止,我有一些视觉效果.我正在使用带有矩形和分隔符的网格来制作网格线.我感到困惑的是我每个月都可以做什么.我希望能够在月份之间切换两个箭头按钮,但我不确定我能做些什么来使正确的日期出现在它们应该出现的位置.我打算使用按钮在日历上显示每个日期,这样当用户点击它时,他/她可以为该特定日期/日期添加一个事件/约会.非常感谢任何建议或提示.

I'm new to vs10 and I'm trying to make a customized schedule/calender using WPF. So far I have somewhat of a visual. I'm using a grid with rectangles and separators to make the grid lines. What I'm confused on is how I can make every month. I want to be able to have two arrow buttons switching between months but I'm not sure what I can do to make the correct dates to appear where they should be. I was going to use buttons to display each date on the calender so when the user clicks on it he/she can add an event/appointment for that particular day/date. Any advice or tips is greatly appreciated.

    Page x:Class="Camp_.SchedulePage"
  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" 
  mc:Ignorable="d" 
  d:DesignHeight="315" d:DesignWidth="587"
Title="schedulePage" Background="#FF0A0A0A">
<Grid Width="Auto">
    <Calendar Height="174" HorizontalAlignment="Left" Margin="190,78,0,0" Name="calendar1" VerticalAlignment="Top" Width="189" />
<Grid>
    <Grid.Background>
        <RadialGradientBrush>
            <GradientStop Color="#FFC3D6F5" Offset="0" />
            <GradientStop Color="#FFEFF5FF" Offset="1" />
        </RadialGradientBrush>
    </Grid.Background>
        <Grid HorizontalAlignment="Stretch" Margin="98,60,0,0" Name="grid1" VerticalAlignment="Stretch" Width="Auto" OpacityMask="Black" Opacity="1" Background="#FFC2ECEC" ShowGridLines="False">
            <Grid.Resources>  <Style x:Key="VerticalSeparatorStyle" 
   TargetType="{x:Type Separator}"
   BasedOn="{StaticResource {x:Type Separator}}">
                <Setter Property="Margin" Value="0,0,0,0"/>
                <Setter Property="LayoutTransform">
                    <Setter.Value>
                        <TransformGroup>
                            <TransformGroup.Children>
                                <TransformCollection>
                                    <RotateTransform Angle="90"/>
                                </TransformCollection>
                            </TransformGroup.Children>
                        </TransformGroup>
                    </Setter.Value>
                </Setter>
                </Style>
            </Grid.Resources>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Separator Grid.ColumnSpan="7" Height="19" HorizontalAlignment="Stretch" Margin="0" Name="separator4" VerticalAlignment="Stretch" Width="Auto" Grid.Row="3" Grid.RowSpan="2" Background="Aqua" HorizontalContentAlignment="Stretch" Foreground="Aqua" OpacityMask="Aqua" />
            <Separator  Height="19" HorizontalAlignment="Stretch" Margin="0" Name="separator2" VerticalAlignment="Stretch" Width="Auto" Background="Aqua" Grid.ColumnSpan="7" Grid.Row="1" Grid.RowSpan="2" HorizontalContentAlignment="Stretch" Foreground="Aqua" />
            <Separator Height="26" HorizontalAlignment="Stretch" Name="separator1" VerticalAlignment="Stretch" Background="Aqua" Grid.Row="2" Grid.ColumnSpan="7" Margin="0" Grid.RowSpan="2" HorizontalContentAlignment="Stretch" Foreground="Aqua" />
            <Separator Height="27" HorizontalAlignment="Stretch" Margin="0" Name="separator3" VerticalAlignment="Stretch" Background="Aqua" Grid.ColumnSpan="7" Grid.RowSpan="2" Width="Auto" HorizontalContentAlignment="Stretch" Foreground="Aqua" />

            <Rectangle Height="Auto" HorizontalAlignment="Stretch" Name="rectangle1" Stroke="Aqua" VerticalAlignment="Stretch" Width="Auto" Grid.RowSpan="5" />
            <Rectangle Grid.RowSpan="5" Height="Auto" HorizontalAlignment="Stretch" Name="rectangle2" Stroke="Aqua" VerticalAlignment="Stretch" Width="Auto" Grid.Column="1" />
            <Rectangle Grid.RowSpan="5" HorizontalAlignment="Stretch" Name="rectangle3" Stroke="Aqua" VerticalAlignment="Stretch" Width="Auto" Grid.Column="2" />
            <Rectangle Grid.RowSpan="5" Height="Auto" HorizontalAlignment="Stretch" Name="rectangle4" Stroke="Aqua" Grid.Column="3" />
            <Rectangle Grid.RowSpan="5" Height="Auto" HorizontalAlignment="Stretch" Name="rectangle5" Stroke="Aqua" Grid.Column="4" Margin="0" Grid.ColumnSpan="2" />
            <Rectangle Grid.RowSpan="5" Height="Auto" HorizontalAlignment="Stretch" Name="rectangle6" Stroke="Aqua" VerticalAlignment="Stretch" Grid.Column="5" />
            <Rectangle Grid.RowSpan="5" Height="Auto" HorizontalAlignment="Stretch" Name="rectangle7" Stroke="Aqua" VerticalAlignment="Stretch" Grid.Column="6" />
            <Button Content="1" Height="20" HorizontalAlignment="Stretch" Name="button1" VerticalAlignment="Top" Width="Auto" Grid.Column="6" HorizontalContentAlignment="Left" />
            <Button Content="2" Height="20" HorizontalAlignment="Stretch" Name="button2" VerticalAlignment="Top" Width="Auto" Grid.Row="1" HorizontalContentAlignment="Left" />
        </Grid>



        <Grid Height="31" HorizontalAlignment="Stretch" Margin="98,31,0,0" Name="grid2" VerticalAlignment="Top" Width="Auto">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <Label Content="Sunday" Height="28" HorizontalAlignment="Stretch" Margin="0,6,0,0" Name="label1" VerticalAlignment="Top" />
            <Label Content="Monday" Height="28" HorizontalAlignment="Stretch" Margin="0,6,0,0" Name="label2" VerticalAlignment="Top" Grid.Column="1" />
            <Label Content="Tuesday" Height="28" HorizontalAlignment="Stretch" Margin="0,6,0,0" Name="label3" VerticalAlignment="Top" Grid.Column="2" />
            <Label Content="Wednesday" Height="28" HorizontalAlignment="Stretch" Margin="0,6,68,0" Name="label4" VerticalAlignment="Top" Grid.Column="3" Grid.ColumnSpan="2" />
            <Label Content="Thursday" Height="28" HorizontalAlignment="Stretch" Margin="0,6,2,0" Name="label5" VerticalAlignment="Top" Grid.Column="4" />
            <Label Content="Friday" Height="28" HorizontalAlignment="Stretch" Margin="0,6,0,0" Name="label6" VerticalAlignment="Top" Grid.Column="5" />
            <Label Content="Saturday" Height="28" HorizontalAlignment="Stretch" Margin="0,6,0,0" Name="label7" VerticalAlignment="Top" Grid.Column="6" />
        </Grid>
        <Button Content="&gt;" Height="23" HorizontalAlignment="Left" Margin="488,6,0,0" Name="button3" VerticalAlignment="Top" Width="29" />
        <Button Content="&lt;" Height="23" HorizontalAlignment="Left" Margin="168,8,0,0" Name="button4" VerticalAlignment="Top" Width="29" />
    </Grid>
</Grid>

推荐答案

通常我会将 Day 对象列表绑定到 ItemsControl,其 ItemsPanel 设置为具有 6 行和 7 列的 Grid,并且让每个 Day 跟踪 WeekWeekDay 它属于,或者让转换器为我找出这些值.然后我可以将这些值绑定到我的 ItemsControl.ItemContainerStyle

Usually I would bind a list of Day objects to an ItemsControl whose ItemsPanel is set to a Grid with 6 rows, and 7 columns, and either have each Day track what Week and WeekDay it belongs to, or have a converter figure out those values for me. Then I could bind those values to Grid.Row and Grid.Column in my ItemsControl.ItemContainerStyle

所以我的 Day 对象看起来像:

So my Day object would look like:

public class Day : INotifyPropertyChanged
{
    // Of course, these should implement the usual PropertyChange Notifications
    public int WeekNo {get; set;}
    public int WeekDay {get; set;}
    public DateTime  Date {get; set;}
}

我会有一个 ObservableCollection 在我的 ViewModel 中,它将在 XAML 中与此绑定:

and I would have an ObservableCollection<Day> Days in my ViewModel, which would be bound in the XAML with this:

<ItemsControl ItemsSource="{Binding Days}">
    <!-- ItemsPanelTemplate -->
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <!-- ItemContainerStyle -->
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Grid.Column" Value="{Binding DayOfWeek}" />
            <Setter Property="Grid.Row" Value="{Binding WeekNo}" />
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>

我写了一些关于如何使用 ItemsControl 的示例此处 如果你有兴趣

I wrote some examples on how to use an ItemsControl here if you're interested

这篇关于在 WPF 中制定自定义计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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