I wanted to create a control like the Numeric Value Editor found in Microsoft Expression Blend which allows you to change the value by dragging the mouse. You can download the source code here (note: change the .doc extension to .zip before extracting the source)
I found that is quite easy, first you need to create a custom control that contains all the increment parameters, described below, the actual value and code to handle the mouse events.
The control exposes the following properties:
- Value – The actual value.
- SmallChange – Small change increments done by dragging the mouse and pressing the CTRL key.
- DefaultChange - Default change increments done by just dragging the mouse.
- LargeChange – Large change increments done by dragging the mouse and pressing the Shift key.
- Minimum - Lower bound of the value.
- Maximum - Upper bound of the value.
- MaxPrecision - Number of digits after the decimal point.
For the control appearance you need to create a Template Style. Below there is an example of a template that contains a TextBox, a Rectangle for the dragging area and a property triggers to change the cursor to notify the user that dragging is available.
<Style x:Key="NumricEditorStyle" TargetType="{x:Type ValueEditorSample:ValueEditor}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ValueEditorSample:ValueEditor}"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="0.5*"/> <ColumnDefinition /> </Grid.ColumnDefinitions> <TextBox x:Name="PART_EDITOR" Text="{Binding Path=Value, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Grid.ColumnSpan="2"/> <Rectangle x:Name="PART_DRAGGER" Stroke="Transparent" Fill="Transparent" /> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True" SourceName="PART_DRAGGER"> <Setter Property="Cursor" Value="SizeAll"/> </Trigger> <Trigger Property="IsDragging" Value="True"> <Setter Property="Cursor" Value="SizeAll"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> |





September 22, 2008 at 9:49 am |
Thanks for sharing a great sample. Do you have any objection for me using it within my open source project “WPF PropertyGrid” (http://www.codeplex.com/wpfpropertygrid)? Thanks in advance, Denis
September 22, 2008 at 10:08 am |
Please, feel free to use it anywhere you want.
BTW you have done an excellent work on the “WPF PropertyGrid”
Regards,
Andrés
October 3, 2008 at 7:47 am |
Thanks a lot, I’ll provide a link to your blog and this very article within the sources related to numeric text editor.
Kind regards, Denis
June 2, 2009 at 4:56 pm |
A simple one yet so powerful. Thanks a ton. Works like a charm
October 18, 2009 at 11:39 pm |
Thanks so much! One thing it is lacking, in my opinion, is an OnChange event. I added one that is raised at the end of MouseMove and it works great!