C# 在WPF中制作定制的时间表
我是vs10的新手,我正在尝试使用WPF定制日程/日历。到目前为止,我有一些视觉效果。我正在使用一个带有矩形和分隔符的网格来生成网格线。让我困惑的是我每个月都能挣多少钱。我希望能够有两个箭头按钮在月份之间切换,但我不确定我能做些什么,使正确的日期出现在它们应该出现的地方。我打算使用按钮在日历上显示每个日期,这样当用户单击日历时,他/她可以为特定的日期/日期添加事件/约会。非常感谢您的任何建议或提示C# 在WPF中制作定制的时间表,c#,wpf,visual-studio,xaml,C#,Wpf,Visual Studio,Xaml,我是vs10的新手,我正在尝试使用WPF定制日程/日历。到目前为止,我有一些视觉效果。我正在使用一个带有矩形和分隔符的网格来生成网格线。让我困惑的是我每个月都能挣多少钱。我希望能够有两个箭头按钮在月份之间切换,但我不确定我能做些什么,使正确的日期出现在它们应该出现的地方。我打算使用按钮在日历上显示每个日期,这样当用户单击日历时,他/她可以为特定的日期/日期添加事件/约会。非常感谢您的任何建议或提示 Page x:Class="Camp_.SchedulePage" xmlns="ht
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=">" Height="23" HorizontalAlignment="Left" Margin="488,6,0,0" Name="button3" VerticalAlignment="Top" Width="29" />
<Button Content="<" Height="23" HorizontalAlignment="Left" Margin="168,8,0,0" Name="button4" VerticalAlignment="Top" Width="29" />
</Grid>
</Grid>
第x页:Class=“Camp\uu.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=“#ff0a0a”>
通常,我会将一个
日
对象列表绑定到一个项控件
,该控件的项面板
设置为一个网格
,有6行7列,或者让每个日
跟踪它所属的周
和周
,或者让一个转换器为我计算出这些值。然后我可以将这些值绑定到我的ItemsControl.itemsContainerStyle中的Grid.Row
和Grid.Column
因此,我的Day
对象看起来像:
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;}
}
在我的ViewModel中,我会有一个ObservableCollection Days
,它在XAML中绑定如下:
<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的例子,这样我就可以创建7个整数天(星期日->星期六),但是我不清楚你是如何说我可以跟踪它所属的工作日(星期)还是如何创建一个转换器。还有什么是itemspanelTemplate?很抱歉我的无知,但这对我来说是全新的,尤其是有约束力的。:)谢谢您的输入。顺便说一句@TMan查看我发布的链接,了解ItemsControl的示例及其工作原理。它将循环遍历一个集合,并将每个项目一次绘制到屏幕上。ItemsPanelTemplate
是容纳所有项目的容器,而itemscontainerStyle
是应用于所有项目的样式。您还可以使用ItemTemplate
来告诉WPF如何绘制每个项目。划掉itemspaneltemplate..刚才在您的博客上看到了这个答案。@TMan我还为我的答案添加了一个示例Day类。您可以使用C#的日期函数来获取DayOfWeek,也可以使用Math.Floor(Date.Day/7)
来获取所有输入的WeekNoThanks,我看看我能做些什么。:)