Getting into the first serious WPF project. It seems like there are a lot of basic controls flat out missing. Specifically, I am looking for the Numeric UpDown control. W
Simply use the IntegerUpDown
control in the xtended wpf toolkit
You can use it like this:
Add to your XAML the following namespace:
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
In your XAML where you want the control use:
<xctk:IntegerUpDown Name="myUpDownControl" />
You can use NumericUpDown control for WPF written by me as a part of WPFControls library.
Use VerticalScrollBar
with the TextBlock
control in WPF. In your code behind, add the following code:
In the constructor, define an event handler for the scrollbar:
scrollBar1.ValueChanged += new RoutedPropertyChangedEventHandler<double>(scrollBar1_ValueChanged);
scrollBar1.Minimum = 0;
scrollBar1.Maximum = 1;
scrollBar1.SmallChange = 0.1;
Then in the event handler, add:
void scrollBar1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
FteHolderText.Text = scrollBar1.Value.ToString();
}
Here is the original snippet from my code... make necessary changes.. :)
public NewProjectPlan()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(NewProjectPlan_Loaded);
scrollBar1.ValueChanged += new RoutedPropertyChangedEventHandler<double>(scrollBar1_ValueChanged);
scrollBar1.Minimum = 0;
scrollBar1.Maximum = 1;
scrollBar1.SmallChange = 0.1;
// etc...
}
void scrollBar1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
FteHolderText.Text = scrollBar1.Value.ToString();
}
Apologize for keep answering 9 years questions.
I have follow @Michael's answer and it works.
I do it as UserControl where I can drag and drop like a Controls elements. I use MaterialDesign Theme from Nuget to get the Chevron icon and button ripple effect.
The running NumericUpDown from Micheal with modification will be as below:-
The code for user control:-
TemplateNumericUpDown.xaml
<UserControl x:Class="UserControlTemplate.TemplateNumericUpDown"
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"
xmlns:local="clr-namespace:UserControlTemplate"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
mc:Ignorable="d" MinHeight="48">
<Grid Background="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="60"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="txtNum" x:FieldModifier="private" Text="{Binding Path=NumValue}" TextChanged="TxtNum_TextChanged" FontSize="36" BorderThickness="0" VerticalAlignment="Center" Padding="5,0"/>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="30*"/>
<RowDefinition Height="30*"/>
</Grid.RowDefinitions>
<Grid Background="#FF673AB7">
<Viewbox HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="Auto" Width="Auto">
<materialDesign:PackIcon Kind="ChevronUp" Foreground="White" Height="32.941" Width="32"/>
</Viewbox>
<Button x:Name="cmdUp" x:FieldModifier="private" Click="CmdUp_Click" Height="Auto" BorderBrush="{x:Null}" Background="{x:Null}"/>
</Grid>
<Grid Grid.Row="1" Background="#FF673AB7">
<Viewbox HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="Auto" Width="Auto">
<materialDesign:PackIcon Kind="ChevronDown" Foreground="White" Height="32.942" Width="32"/>
</Viewbox>
<Button x:Name="cmdDown" x:FieldModifier="private" Click="CmdDown_Click" Height="Auto" BorderBrush="{x:Null}" Background="{x:Null}"/>
</Grid>
</Grid>
</Grid>
</UserControl>
TemplateNumericUpDown.cs
using System.Windows;
using System.Windows.Controls;
namespace UserControlTemplate
{
/// <summary>
/// Interaction logic for TemplateNumericUpDown.xaml
/// </summary>
public partial class TemplateNumericUpDown : UserControl
{
private int _numValue = 0;
public TemplateNumericUpDown()
{
InitializeComponent();
txtNum.Text = _numValue.ToString();
}
public int NumValue
{
get { return _numValue; }
set
{
if (value >= 0)
{
_numValue = value;
txtNum.Text = value.ToString();
}
}
}
private void CmdUp_Click(object sender, RoutedEventArgs e)
{
NumValue++;
}
private void CmdDown_Click(object sender, RoutedEventArgs e)
{
NumValue--;
}
private void TxtNum_TextChanged(object sender, TextChangedEventArgs e)
{
if (txtNum == null)
{
return;
}
if (!int.TryParse(txtNum.Text, out _numValue))
txtNum.Text = _numValue.ToString();
}
}
}
On MyPageDesign.xaml, drag and drop created usercontrol will having <UserControlTemplate:TemplateNumericUpDown HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100"/>
To get the value from the template, I use
string Value1 = JournalNumStart.NumValue;
string Value2 = JournalNumEnd.NumValue;
I'm not in good skill yet to binding the Height of the control based from FontSize element, so I set the from my page fontsize manually in usercontrol.
** Note:- I have change the "Archieve" name to Archive on my program =)
Just a pragmatic to do sample:
-Right click your Project (under Solution), select "Manage nuget Packages..."
-In Menu click Browse Tab search for "wpftoolkit", select "Extended.Wpf.Toolkit
"
-Install it!
-Right click in your User Control Toolbox, select "Add Tab.." and name it "WPF Toolkit"
-Right click on the new "WPF Toolkit" Tab, select "Choose items..."
-In Menu click "Browse..." Button, look for nugets DLL folder, select all
"...\packages\Extended.Wpf.Toolkit.3.5.0\lib\net40\*.dll
"
Ignore Warnings about some DLLs may not containing user controls!
Ready :)
The given answers are OK. However, I wanted the buttons to auto hide, when mouse leave the control. Here is my code based on vercin answer above:
Style
<Style TargetType="{x:Type v:IntegerTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type v:IntegerTextBox}">
<Grid Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBox Name="tbmain" Grid.ColumnSpan="2" Grid.RowSpan="2"
Text="{Binding Value, Mode=TwoWay, NotifyOnSourceUpdated=True,
NotifyOnValidationError=True, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type v:IntegerTextBox}}}"
Style="{StaticResource ValidationStyle}" />
<RepeatButton Name="PART_UpButton" BorderThickness="0" Grid.Column="1" Grid.Row="0"
Width="13" Background="Transparent">
<Path Fill="Black" Data="M 0 3 L 6 3 L 3 0 Z"/>
</RepeatButton>
<RepeatButton Name="PART_DownButton" BorderThickness="0" Grid.Column="1" Grid.Row="1"
Width="13" Background="Transparent">
<Path Fill="Black" Data="M 0 0 L 3 3 L 6 0 Z"/>
</RepeatButton>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Visibility" TargetName="PART_UpButton" Value="Collapsed"/>
<Setter Property="Visibility" TargetName="PART_DownButton" Value="Collapsed"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Code
public partial class IntegerTextBox : UserControl
{
public IntegerTextBox()
{
InitializeComponent();
}
public int Maximum
{
get { return (int)GetValue(MaximumProperty); }
set { SetValue(MaximumProperty, value); }
}
public readonly static DependencyProperty MaximumProperty = DependencyProperty.Register(
"Maximum", typeof(int), typeof(IntegerTextBox), new UIPropertyMetadata(int.MaxValue));
public int Minimum
{
get { return (int)GetValue(MinimumProperty); }
set { SetValue(MinimumProperty, value); }
}
public readonly static DependencyProperty MinimumProperty = DependencyProperty.Register(
"Minimum", typeof(int), typeof(IntegerTextBox), new UIPropertyMetadata(int.MinValue));
public int Value
{
get { return (int)GetValue(ValueProperty); }
set { SetCurrentValue(ValueProperty, value); }
}
public readonly static DependencyProperty ValueProperty = DependencyProperty.Register(
"Value", typeof(int), typeof(IntegerTextBox), new UIPropertyMetadata(0, (o,e)=>
{
IntegerTextBox tb = (IntegerTextBox)o;
tb.RaiseValueChangedEvent(e);
}));
public event EventHandler<DependencyPropertyChangedEventArgs> ValueChanged;
private void RaiseValueChangedEvent(DependencyPropertyChangedEventArgs e)
{
ValueChanged?.Invoke(this, e);
}
public int Step
{
get { return (int)GetValue(StepProperty); }
set { SetValue(StepProperty, value); }
}
public readonly static DependencyProperty StepProperty = DependencyProperty.Register(
"Step", typeof(int), typeof(IntegerTextBox), new UIPropertyMetadata(1));
RepeatButton _UpButton;
RepeatButton _DownButton;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_UpButton = Template.FindName("PART_UpButton", this) as RepeatButton;
_DownButton = Template.FindName("PART_DownButton", this) as RepeatButton;
_UpButton.Click += btup_Click;
_DownButton.Click += btdown_Click;
}
private void btup_Click(object sender, RoutedEventArgs e)
{
if (Value < Maximum)
{
Value += Step;
if (Value > Maximum)
Value = Maximum;
}
}
private void btdown_Click(object sender, RoutedEventArgs e)
{
if (Value > Minimum)
{
Value -= Step;
if (Value < Minimum)
Value = Minimum;
}
}
}