分類:[.NET 全般]
2009/05/26(Tue) 21:46:35 編集(投稿者)
こんばんは、倉田 有大です。
<UserControl x:Class="SkypeTRPGToolWPF.numericUpDown"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="25" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock FontSize="20" Grid.RowSpan="2" Name="textBlock1" Grid.ColumnSpan="2" />
<Button Grid.Row="0" Grid.Column="1" Height ="Auto" HorizontalAlignment="Right" Margin="0" Name="button1" Width="23" Click="button1_Click" VerticalAlignment="Top">↑</Button>
<Button Grid.Row="1" Grid.Column="1" Height ="Auto" HorizontalAlignment="Right" Name="button2" Width="23" VerticalAlignment="Bottom" Click="button2_Click">↓</Button>
<Border BorderThickness="1" Grid.RowSpan="2" BorderBrush ="#FF7F9DB9" Width="auto" Height="Auto"/>
</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;
namespace SkypeTRPGToolWPF
{
/// <summary>
/// numericUpDown.xaml の相互作用ロジック
/// </summary>
public partial class numericUpDown : UserControl, INotifyPropertyChanged
{
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(int), typeof(numericUpDown));
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var h = PropertyChanged;
if (h == null) return;
h(this, new PropertyChangedEventArgs(propertyName));
}
private int _Value;
public int Maximum
{
set;
get;
}
public int Mininum
{
set;
get;
}
public int Value
{
set
{
this.textBlock1.Text = value.ToString();
_Value = value;
OnPropertyChanged("Value");
SetValue(ValueProperty, value);
}
get
{
return _Value;
}
}
public numericUpDown()
{
Maximum = 100;
Mininum = 0;
InitializeComponent();
Value = 2;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (Value < Maximum)
{
Value++;
this.textBlock1.Text = Value.ToString();
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
if (Value > Mininum)
{
Value--;
this.textBlock1.Text = Value.ToString();
}
}
}
}
上記のような、numericUpDownライクのコントロールを作成しました。
メインのウインドウに
<local:numericUpDown Value = "{Binding Path = DiceNum}" Width="auto" Grid.Row="2" Grid.Column="1" x:Name="numericUpDown1" />
上記のように貼り付けたのですが、ユーザーコントロールのPropertyChangedがnullのままでイベントが起こってくれません。
PropertyChangedにはどのタイミングで値が入るのでしょうか?