東方中央幻視台

瞼の裏に弾幕が飛び交う程度の能力 - 永夜抄Normalはそれなりに難しい。

目次

Blog 利用状況

ニュース

自己紹介

関連サイト

投稿カレンダー

  • スクリプトをダウンロード!

ブログパーツ

Adsense

書庫

日記カテゴリ

[WPF]自作NumericUpDownでバインディングが効かない

ButtonとTextBoxで構成されたNumericUpDownを作ったんだが、
どうもデータバインドが効いてないっぽい。

手動設定すれば動くんだが、何か規格違反があるのかな?

<UserControl x:Class="WasaWasa.Controls.NumericUpDown"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ws="clr-namespace:WasaWasa.Converters;assembly=WasaWasa.Converters"
    MinWidth="50" MinHeight="20">
    <UserControl.Resources>
        <ws:IsDecimalRule x:Key="isDecimalRule"/>
        <ws:DecimalRangeValidationRule x:Key="decimalRangeValidationRule"/>
    </UserControl.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="16"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="16"/>
        </Grid.ColumnDefinitions>
        <Button Name="btnDown" Grid.Column="0" Margin="0">-</Button>
        <TextBox Name="txtValue" Grid.Column="1" TextAlignment="Right" Margin="0">
            <Binding x:Name="binding"  Mode="TwoWay" Path="Value" UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules>
                    <StaticResource ResourceKey="isDecimalRule" />
                    <StaticResource ResourceKey="decimalRangeValidationRule" />
                </Binding.ValidationRules>
            </Binding>
        </TextBox>
        <Button Name="btnUp" Grid.Column="2" Margin="0">+</Button>
    </Grid>
</UserControl>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using WasaWasa.Converters;

namespace WasaWasa.Controls
{
    /// <summary>
    /// NumericUpDown.xaml の相互作用ロジック
    /// </summary>
    //TODO:MinValueとMaxValueとStepValueを依存プロパティ化する
    public partial class NumericUpDown : UserControl, INotifyPropertyChanged
    {
        #region INotifyPropertyChanged メンバ
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        private static ValidationRule integerRule = new DecimalInInt32ValidationRule();

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(decimal), typeof(NumericUpDown),
            new PropertyMetadata(new PropertyChangedCallback(delegate(DependencyObject p, DependencyPropertyChangedEventArgs e)
        {
            if ((p as NumericUpDown).PropertyChanged != null)
                (p as NumericUpDown).PropertyChanged((p as NumericUpDown), new PropertyChangedEventArgs("Value"));
        })));

        public static readonly DependencyProperty IsIntegerOnlyProperty =
            DependencyProperty.Register("IsIntegerOnly", typeof(bool), typeof(NumericUpDown),
            new PropertyMetadata(new PropertyChangedCallback(delegate(DependencyObject p, DependencyPropertyChangedEventArgs e)
        {
            NumericUpDown ctrl = p as NumericUpDown;
            bool value = (bool)e.NewValue;
            bool oldValue = (bool)e.OldValue;

            if (value)
            {
                if (oldValue)
                    return;
                ctrl.AddValidationRule(NumericUpDown.integerRule);
            }
            else
            {
                if (!oldValue)
                    return;
                ctrl.RemoveValidationRule(NumericUpDown.integerRule);
            }
        })));

        public static readonly DependencyProperty MinValueProperty =
            DependencyProperty.Register("MinValue", typeof(decimal), typeof(NumericUpDown),
            new PropertyMetadata(decimal.MinValue, new PropertyChangedCallback(delegate(DependencyObject p, DependencyPropertyChangedEventArgs e)
        {
            ((p as NumericUpDown).FindResource("decimalRangeValidationRule") as DecimalRangeValidationRule).MinValue = (decimal)e.NewValue;
            if ((p as NumericUpDown).Value < (decimal)e.NewValue)
                (p as NumericUpDown).Value = (decimal)e.NewValue;
        })));

        public static readonly DependencyProperty MaxValueProperty =
            DependencyProperty.Register("MaxValue", typeof(decimal), typeof(NumericUpDown),
            new PropertyMetadata(decimal.MaxValue, new PropertyChangedCallback(delegate(DependencyObject p, DependencyPropertyChangedEventArgs e)
        {
            ((p as NumericUpDown).FindResource("decimalRangeValidationRule") as DecimalRangeValidationRule).MaxValue = (decimal)e.NewValue;
            if ((p as NumericUpDown).Value > (decimal)e.NewValue)
                (p as NumericUpDown).Value = (decimal)e.NewValue;
        })));

        public static readonly DependencyProperty StepValueProperty =
            DependencyProperty.Register("StepValue", typeof(decimal), typeof(NumericUpDown),
            new PropertyMetadata(1M), new ValidateValueCallback(delegate(object o)
        {
            return (decimal)o > 0;
        }));

        public bool IsIntegerOnly
        {
            get { return (bool)GetValue(IsIntegerOnlyProperty); }
            set
            {
                SetValue(IsIntegerOnlyProperty, value);

                PropertyChanged(this, new PropertyChangedEventArgs(ValueProperty.Name));
            }
        }

        public decimal Value
        {
            get { return (decimal)GetValue(ValueProperty); }
            set
            {
                if (value < MinValue)
                    value = MinValue;
                if (MaxValue < MaxValue)
                    value = MinValue;
                SetValue(ValueProperty, value);
            }
        }

        public decimal MinValue
        {
            get { return (decimal)GetValue(MinValueProperty); }
            set
            {
                SetValue(MinValueProperty, value);
            }
        }

        public decimal MaxValue
        {
            get { return (decimal)GetValue(MaxValueProperty); }
            set
            {
                SetValue(MinValueProperty, value);
            }
        }

        public decimal StepValue
        {
            get { return (decimal)GetValue(StepValueProperty); }
            set
            {
                SetValue(StepValueProperty, value);
            }
        }

        public NumericUpDown()
        {
            InitializeComponent();
            DataContext = this;
            btnUp.Click += new RoutedEventHandler(delegate(object sender, RoutedEventArgs e)
            {
                Value += StepValue;
            });
            btnDown.Click += new RoutedEventHandler(delegate(object sender, RoutedEventArgs e)
            {
                Value -= StepValue;
            });
        }

        public void AddValidationRule(ValidationRule rule)
        {
            binding.ValidationRules.Add(rule);
        }

        public void RemoveValidationRule(ValidationRule rule)
        {
            binding.ValidationRules.Remove(rule);
        }
    }
}

投稿日時 : 2008年7月6日 15:38

コメントを追加

# re: [WPF]自作NumericUpDownでバインディングが効かない 2008/07/06 16:45 えムナウ

BindableAttribute クラス をどうぞ

>手動設定すれば動くんだが、何か規格違反があるのかな?
手動設定って何?

# re: [WPF]自作NumericUpDownでバインディングが効かない 2008/07/06 16:50 えムナウ

>(p as NumericUpDown).PropertyChanged
CAST と as is をもう一度勉強しなおした方がいいですよ。

# re: [WPF]自作NumericUpDownでバインディングが効かない 2008/07/06 17:56 Hirotow

>DataContext = this;
これが原因だったようです。
txtValue.DataContext = this;
にしたらうまくいきました。

# Nike Free 3.0 2012/10/31 13:24 http://www.nikefree3runschuhe.com/

If you need an shipping with the deserving of, rely friends and family.
Nike Free 3.0 http://www.nikefree3runschuhe.com/

# Nike Free 5.0 Frauen 2012/10/31 13:24 http://www.nikefree3runschuhe.com/nike-free-run-fr

If you might choosing the best top-secret coming from an opponent, tell them do not a fellow worker.
Nike Free 5.0 Frauen http://www.nikefree3runschuhe.com/nike-free-run-frauen.html/

# Nike Free Run 2 Herren 2012/10/31 13:25 http://www.nikefree3runschuhe.com/nike-free-run-he

An absense of individual may be worth your rips, as well a person who is in fact picked up‘s make you shout.
Nike Free Run 2 Herren http://www.nikefree3runschuhe.com/nike-free-run-herren.html/

# mia clarisonic mia coupon 2012/10/31 13:25 http://www.clarisonicmia-coupon.com/

Will be previous the moment the two companion seems bigger a slight transcendence during the all the other.
mia clarisonic mia coupon http://www.clarisonicmia-coupon.com/

# moncler outlet 2012/12/08 4:19 http://www.moncleroutletstore.eu/

Compelling he certainly is but, as Sutherland indicated (I will be more explicit), only to Westerners and Americans in particular

# destockchine site 2013/01/11 3:00 http://www.destockchinefr.fr/

A genuine acquaintance is actually the people that overlooks your primary setbacks and even can handle your primary positive results.
destockchine site http://www.destockchinefr.fr/

# www.b66.fr 2013/02/27 3:01 http://www.b66.fr/

At money a lot of our classmates and friends comprehend mankind; at adversity children a lot of our classmates and friends. www.b66.fr http://www.b66.fr/

# Jordan Retro 3 2013/03/04 13:58 http://www.jordanretro3air.com/

Found in plethora some of our great friends discover people; within trouble could some of our great friends. Jordan Retro 3 http://www.jordanretro3air.com/

# www.c55.fr 2013/03/04 13:58 http://www.c55.fr/

Absolutely adore can be frail available at start off, it will becomes more forceful as we grow older if at all thoroughly federal reserve. www.c55.fr http://www.c55.fr/

# casquette la 2013/03/04 13:59 http://www.b66.fr/

Association is most likely the golden thread that experts claim neckties the exact hearts with the earth. casquette la http://www.b66.fr/

# destockprix 2013/03/04 14:02 http://www.f77.fr/

Some uncle might a mate, on the contrary a mate will almost always be some sort of uncle. destockprix http://www.f77.fr/

# code promo la redoute 2013/03/04 14:04 http://www.k88.fr/

Absolutely love is usually imperfect into having your baby, though it springs up more forceful as we grow older if correctly provided with. code promo la redoute http://www.k88.fr/

# marqueWeb 2013/03/04 14:48 http://www.g33.fr/

Serious relationship foresees the needs of several rather of extol it can be particular. marqueWeb http://www.g33.fr/

# Air Jordan Retro 4 2013/03/04 14:48 http://www.jordanretro4air.com/

Put on‘p consider so difficult, the perfect objects can be bought whenever you minimum , hope for these types of. Air Jordan Retro 4 http://www.jordanretro4air.com/

# Air Jordan Retro 3 2013/03/06 11:29 http://www.jordanretro3air.com/

A genuine buddie certainly one who overlooks your current downfalls and furthermore can handle your current positive results. Air Jordan Retro 3 http://www.jordanretro3air.com/

# Shoes.fr 2013/03/17 2:12 http://www.c88.fr/

Really enjoy may be the athletic anxiety for a your life in addition to development of that which our organization really like. Shoes.fr http://www.c88.fr/

# tee shirt superman 2013/03/18 4:37 http://www.i77.fr/

It's the best happened credited what you do, but nevertheless , credited so , who I am certain after was in hand. tee shirt superman http://www.i77.fr/

# destockchine 2013/03/23 19:13 http://d77.fr/

Companionship is going to be goldthread which experts claim scarves those minds dried up country. destockchine http://d77.fr/

# usine23 2013/03/26 0:48 http://e55.fr/

Put on‘MT use so faithfully, the optimal tasks take place should you minimum , be expecting these. usine23 http://e55.fr/

# destockchine 2013/03/26 0:48 http://c99.fr/

Will be carry on anytime equally roommate is certain brand-new areas such as a slight brilliance within the some other. destockchine http://c99.fr/

# maillot de bain pas cher 2013/04/03 17:30 http://www.chaussuresnike2013.com/

To the world you might be one person, nonetheless one people you might be the world. maillot de bain pas cher http://www.chaussuresnike2013.com/

# dgjGSzydkaNW 2022/04/19 11:46 johnanz

http://imrdsoacha.gov.co/silvitra-120mg-qrms

# jatzazjbrxuw 2022/05/31 17:41 yslskypa

erythromycin ophthalmic https://erythromycin1m.com/#

# 1% ivermectin https://stromectolbestprice.com/ 2022/07/30 8:23 BestPrice

1% ivermectin https://stromectolbestprice.com/

# cheap pet meds without vet prescription https://withoutdoctorprescription.xyz/
online prescription for ed meds 2022/09/07 17:37 IvanPres

cheap pet meds without vet prescription https://withoutdoctorprescription.xyz/
online prescription for ed meds

# buy cheap doxycycline online https://antibiotic.best/ 2022/10/08 8:54 Antibiotic

buy cheap doxycycline online https://antibiotic.best/

# farmacia senza ricetta recensioni https://viasenzaricetta.com/# 2023/04/16 13:02 ViaSenza

farmacia senza ricetta recensioni https://viasenzaricetta.com/#

# ï»¿cytotec pills online - https://cytotecsale.pro/# 2023/04/29 4:34 Cytotec

cytotec pills online - https://cytotecsale.pro/#

# over the counter diuretics https://overthecounter.pro/# 2023/05/08 22:37 OtcJikoliuj

over the counter diuretics https://overthecounter.pro/#

# best otc ed pills https://edpillsotc.store/# - best treatment for ed 2023/10/08 1:03 EdPills

best otc ed pills https://edpillsotc.store/# - best treatment for ed

# cheap valtrex online https://valtrex.auction/ over the counter valtrex cream 2023/10/24 22:02 Valtrex

cheap valtrex online https://valtrex.auction/ over the counter valtrex cream

# paxlovid india https://paxlovid.bid/ Paxlovid buy online 2023/10/25 22:49 Paxlovid

paxlovid india https://paxlovid.bid/ Paxlovid buy online

# where to get doxycycline https://doxycycline.forum/ buy cheap doxycycline 2023/11/25 13:08 Doxycycline

where to get doxycycline https://doxycycline.forum/ buy cheap doxycycline

# farmacia online senza ricetta https://farmaciait.pro/ comprare farmaci online all'estero 2023/12/04 10:08 Farmacia

farmacia online senza ricetta https://farmaciait.pro/ comprare farmaci online all'estero

# abella danger izle https://abelladanger.online/ abella danger filmleri
2024/03/06 10:58 Adella

abella danger izle https://abelladanger.online/ abella danger filmleri

# buy cytotec online https://cytotec.club/ purchase cytotec 2024/04/28 2:49 Cytotec

buy cytotec online https://cytotec.club/ purchase cytotec

タイトル
名前
URL
コメント