http://www.your-assist.ru/Files/WPFUserControlLib.rar http://narod.ru/disk/23530854000/WPFUserControlLib.rar.html
Здравствуйте. Вот делаю UserControl dll'ку. Проблема в том, что один контрол (ColorDialog) там работает. А второй (numericUpDown) там не то что не работает. Я добавляю numericUpDown в окно и добавляется просто пустой UserControl. Не понимаю почему. Уже заколебался разбираться. Программу не пишу толком из-за отсутствия numericUpDown. Слышал можно сделать шаблон для Slider и будет numericUpDown, но что-то я не понял как его делать. Вопрос в том, из-за чего не видно контрола? Может кто нибудь сказать?
| Код | <UserControl x:Class="UserControls.NumericUpDown" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="26" d:DesignWidth="60"> <Grid> <Border BorderBrush="Silver" BorderThickness="1" Name="border1" CornerRadius="5"> <Grid Height="Auto" Name="grid1" Width="Auto"> <Grid.ColumnDefinitions> <ColumnDefinition Width="156*" /> <ColumnDefinition Width="21" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="25*" /> <RowDefinition Height="25*" /> </Grid.RowDefinitions> <TextBlock Text="0" Name="textValue" Margin="2,0" FontSize="14" Grid.RowSpan="2" HorizontalAlignment="Stretch"/> <Button Margin="0,0,1,0" Name="btnUp" HorizontalAlignment="Right" Width="20" Grid.Column="1" /> <Button Margin="0,0,1,0" Name="btnDown" HorizontalAlignment="Right" Width="20" Grid.Row="1" Grid.Column="1" /> </Grid> </Border> </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;
namespace UserControls { /// <summary> /// Логика взаимодействия для NumericUpDown.xaml /// </summary> public partial class NumericUpDown : System.Windows.Controls.UserControl { /// <summary> /// Initializes a new instance of the NumericUpDownControl. /// </summary> public NumericUpDown() { InitializeCommands(); MinimumValue = 0; MaximumValue = 100; }
static NumericUpDown() { DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericUpDown), new FrameworkPropertyMetadata(typeof(NumericUpDown))); }
/// <summary> /// Gets or sets the value assigned to the control. /// </summary> public decimal Value { get { return (decimal)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } }
/// <summary> /// Identifies the Value dependency property. /// </summary> public static readonly DependencyProperty ValueProperty = DependencyProperty.Register( "Value", typeof(decimal), typeof(NumericUpDown), new FrameworkPropertyMetadata((decimal)0, 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(control.MinimumValue, Math.Min(control.MaximumValue, 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); } /// <summary> /// Identifies the ValueChanged routed event. /// </summary> public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent( "ValueChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<decimal>), typeof(NumericUpDown));
/// <summary> /// Occurs when the Value property changes. /// </summary> public event RoutedPropertyChangedEventHandler<decimal> ValueChanged { add { AddHandler(ValueChangedEvent, value); } remove { RemoveHandler(ValueChangedEvent, value); } } /// <summary> /// Raises the ValueChanged event. /// </summary> /// <param name="args">Arguments associated with the ValueChanged event.</param> 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
public decimal MaximumValue { get { return (decimal)GetValue(MaxValueProperty); } set { SetValue(MaxValueProperty, value); } }
public DependencyProperty MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(decimal), typeof(NumericUpDown));
public decimal MinimumValue { get { return (decimal)GetValue(MinValueProperty); } set { SetValue(MinValueProperty, value); } }
public DependencyProperty MinValueProperty = DependencyProperty.Register("MinValue", typeof(decimal), typeof(NumericUpDown));
//private decimal MinimumValue = 0, MaximumValue = 100; } }
|
|