Здравствуйте. Вот поделился со мной 1 человек компонентом NumericUpDown для WPF. Выглядит убого (самостоятельно нужно делать шаблон), но зато работает как надо. Вот я не могу никак сделать так, чтобы можно было менять MinValue и MaxValue. Можно скачать проект| Код | using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input;
namespace CustomControlLibrary { public partial class NumericUpDown : Control {
public NumericUpDown() { InitializeCommands(); }
static NumericUpDown() { DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericUpDown), new FrameworkPropertyMetadata(typeof(NumericUpDown))); }
public static decimal Value { get { return (decimal)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } }
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register( "Value", typeof(decimal), typeof(NumericUpDown), new FrameworkPropertyMetadata(MinValue, new PropertyChangedCallback(OnValueChanged), new CoerceValueCallback(CoerceValue)));
private static object CoerceValue(DependencyObject element, object value) { decimal newValue = (decimal)value; NumericUpDown control = (NumericUpDown)element;
newValue = Math.Max(MinValue, Math.Min(MaxValue, newValue));
return newValue; }
private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { NumericUpDown control = (NumericUpDown)obj;
RoutedPropertyChangedEventArgs<decimal> e = new RoutedPropertyChangedEventArgs<decimal>( (decimal)args.OldValue, (decimal)args.NewValue, ValueChangedEvent); control.OnValueChanged(e); }
public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent( "ValueChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<decimal>), typeof(NumericUpDown));
public event RoutedPropertyChangedEventHandler<decimal> ValueChanged { add { AddHandler(ValueChangedEvent, value); } remove { RemoveHandler(ValueChangedEvent, value); } }
protected virtual void OnValueChanged(RoutedPropertyChangedEventArgs<decimal> args) { RaiseEvent(args); }
#region Commands
public static RoutedCommand IncreaseCommand { get { return _increaseCommand; } } public static RoutedCommand DecreaseCommand { get { return _decreaseCommand; } }
private static void InitializeCommands() { _increaseCommand = new RoutedCommand("IncreaseCommand", typeof(NumericUpDown)); CommandManager.RegisterClassCommandBinding(typeof(NumericUpDown), new CommandBinding(_increaseCommand, OnIncreaseCommand)); CommandManager.RegisterClassInputBinding(typeof(NumericUpDown), new InputBinding(_increaseCommand, new KeyGesture(Key.Up)));
_decreaseCommand = new RoutedCommand("DecreaseCommand", typeof(NumericUpDown)); CommandManager.RegisterClassCommandBinding(typeof(NumericUpDown), new CommandBinding(_decreaseCommand, OnDecreaseCommand)); CommandManager.RegisterClassInputBinding(typeof(NumericUpDown), new InputBinding(_decreaseCommand, new KeyGesture(Key.Down))); }
private static void OnIncreaseCommand(object sender, ExecutedRoutedEventArgs e) { NumericUpDown control = sender as NumericUpDown; if (control != null) { control.OnIncrease(); } } private static void OnDecreaseCommand(object sender, ExecutedRoutedEventArgs e) { NumericUpDown control = sender as NumericUpDown; if (control != null) { control.OnDecrease(); } }
protected virtual void OnIncrease() { Value++; } protected virtual void OnDecrease() { Value--; }
private static RoutedCommand _increaseCommand; private static RoutedCommand _decreaseCommand; #endregion
private const decimal MinValue = 0, MaxValue = 100; } }
|
Надо, чтобы для каждого экземпляра этого объекта задавались свои значения. Потом выложу сюда готовый компонент. Главное помогите кто нибудь. Изменение этого: | Код | private const decimal MinValue = 0, MaxValue = 100;
|
На это: | Код | private decimal MinValue = 0, MaxValue = 100;
|
Не помогает.
|