< Microsoft SQL Server Express の認証モード | NULL 許容型 (Nullable) >

January 28, 2009

連番のプロパティを持つ Items を Grid 上に整列させるコンバータ

public class NumberGridConverter: DependencyObject, IValueConverter
{
    /// <summary>
    /// <c>BaseNumber</c> 依存関係プロパティ
    /// </summary>
    /// <value><c>NumberGridConverter.BaseNumber</c> 属性値</value>
    public int BaseNumber
    {
        get { return (int) GetValue (BaseNumberProperty); }
        set { SetValue (BaseNumberProperty, value); }
    }

    /// <summary><c>BaseNumber</c> 依存関係プロパティを識別します。</summary>
    public static readonly DependencyProperty BaseNumberProperty =
        DependencyProperty.Register ("BaseNumber", typeof (int), typeof (NumberGridConverter), new FrameworkPropertyMetadata ((int) 0, FrameworkPropertyMetadataOptions.AffectsRender));

    /// <summary>
    /// <c>Columns</c> 依存関係プロパティ
    /// </summary>
    /// <value><c>NumberGridConverter.Columns</c> 属性値</value>
    public int Columns
    {
        get { return (int) GetValue (ColumnsProperty); }
        set { SetValue (ColumnsProperty, value); }
    }

    /// <summary><c>Columns</c> 依存関係プロパティを識別します。</summary>
    public static readonly DependencyProperty ColumnsProperty =
        DependencyProperty.Register ("Columns", typeof (int), typeof (NumberGridConverter), new FrameworkPropertyMetadata ((int) 1, FrameworkPropertyMetadataOptions.AffectsRender));

    /// <summary>
    /// <c>Rows</c> 依存関係プロパティ
    /// </summary>
    /// <value><c>NumberGridConverter.Rows</c> 属性値</value>
    public int Rows
    {
        get { return (int) GetValue (RowsProperty); }
        set { SetValue (RowsProperty, value); }
    }

    /// <summary><c>Rows</c> 依存関係プロパティを識別します。</summary>
    public static readonly DependencyProperty RowsProperty =
        DependencyProperty.Register ("Rows", typeof (int), typeof (NumberGridConverter), new FrameworkPropertyMetadata ((int) 1, FrameworkPropertyMetadataOptions.AffectsRender));

    /// <summary>
    /// <c>Orientation</c> 依存関係プロパティ
    /// </summary>
    /// <value><c>NumberGridConverter.Orientation</c> 属性値</value>
    public System.Windows.Controls.Orientation Orientation
    {
        get { return (System.Windows.Controls.Orientation) GetValue (OrientationProperty); }
        set { SetValue (OrientationProperty, value); }
    }

    /// <summary><c>Orientation</c> 依存関係プロパティを識別します。</summary>
    public static readonly DependencyProperty OrientationProperty =
        DependencyProperty.Register ("Orientation", typeof (System.Windows.Controls.Orientation), typeof (NumberGridConverter), new FrameworkPropertyMetadata (System.Windows.Controls.Orientation.Horizontal, FrameworkPropertyMetadataOptions.AffectsRender));


    #region IValueConverter メンバ

    public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var param = parameter as string;
        var number = (int) value - this.BaseNumber;
        if (param != null)
        {
            switch (param.ToUpper ())
            {
                case "COLUMN":
                    switch (this.Orientation)
                    {
                        case System.Windows.Controls.Orientation.Horizontal:
                            return (number % this.Columns);
                        case System.Windows.Controls.Orientation.Vertical:
                            return (number / this.Rows);
                        default:
                            break;
                    }
                    break;

                case "ROW":
                    switch (this.Orientation)
                    {
                        case System.Windows.Controls.Orientation.Horizontal:
                            return (number / this.Columns);
                        case System.Windows.Controls.Orientation.Vertical:
                            return (number % this.Rows);
                        default:
                            break;
                    }
                    break;

                default:
                    break;
            }
        }
        return DependencyProperty.UnsetValue;
    }

    public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException ();
    }

    #endregion
}

トラックバック

このエントリーにトラックバック:
http://frog.raindrop.jp/cgi-bin/mt/mt-tb.cgi/2306

コメント

コメントする

※ コメントスパム対策のため、コメント本文はおはよう、こんにちわ、こんばんわのいずれかより始めるようにしてください。

name:
email:

※ 必要ですが、表示しません。

url:
情報を保存する ?