开发者

Howto create a ordered WPF multimonth index view?

开发者 https://www.devze.com 2023-02-10 18:51 出处:网络
Is it possible to declare a (linked) multimonth calendar Index View Control in pure Xaml? Disregarding the public interface of the resulting control.

Is it possible to declare a (linked) multimonth calendar Index View Control in pure Xaml? Disregarding the public interface of the resulting control.

What i intend to get would look like this (well, kind of):

<January> <February> <..>

1234556   12345678

78910 ..    91011...

Above you see two Calendars. If one of those calendars is switched forward or backward one month, the "neighbours" also have to switch state in that direction. Additionally the number of Visible Calendars should be determined by the current width and he开发者_如何转开发ight of the control.

First, i was thinking of a WrapPanel. But what i try to find out is how to specifiy the itemssource (list of DateTime's) and the "link" between the calendars.

Any suggestions on how to solve this in best WPF manner?


You can link the calendars by binding the DisplayDate value to its adjacent Calendar control, and using a Value Converter to adjust the month. I've include a basic prototype to get you started.

Here is the XAML:

<Window x:Class="CalendarTriggers.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:Converter="clr-namespace:CalendarTriggers" 
    Title="MainWindow" 
    Height="350" Width="525">
  <Window.Resources>
    <Converter:CalendarConverter x:Key="conv"/>
  </Window.Resources>
  <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Calendar x:Name="Month1" Grid.Column="0" DisplayDate="2011/01/11" />
    <Calendar x:Name="Month2" Grid.Column="1" DisplayDate="{Binding Path=DisplayDate, ElementName=Month1,  Converter={StaticResource conv}}" />
    <Calendar x:Name="Month3" Grid.Column="2" DisplayDate="{Binding Path=DisplayDate, ElementName=Month2,  Converter={StaticResource conv}}"/>
  </Grid>
</Window>

Here is the converter:

using System;
using System.Globalization;
using System.Windows.Data;

namespace CalendarTriggers
{
  public class CalendarConverter : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      DateTime input = DateTime.Now;
      try
      {
        input = (DateTime)value;
        if (input != null)
        {
          input = input.AddMonths(1);
        }
      }
      catch (Exception)
      {
      }  
      return input;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      DateTime input = DateTime.Now;
      try
      {
        input = (DateTime)value;
        if (input != null)
        {
          input = input.AddMonths(-1);
        }
      }
      catch (Exception)
      {
      }
      return input;
    }
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消