Controls: Add UnitTextBox

This commit is contained in:
2023-11-04 21:40:54 +01:00
parent 12226c3d0f
commit 6435a649b4
12 changed files with 118 additions and 57 deletions

View File

@ -1,16 +1,17 @@
using System;
using System.Windows.Data;
using System.Globalization;
namespace Elwig.Controls {
public class BoolToValueConverter<T> : IValueConverter {
public T FalseValue { get; set; }
public T TrueValue { get; set; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
return (bool)value ? TrueValue : FalseValue;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
return value?.Equals(TrueValue) ?? false;
}
}

View File

@ -0,0 +1,11 @@
<UserControl x:Class="Elwig.Controls.UnitTextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="25">
<TextBox x:Name="TextBox" TextAlignment="Right" FontSize="14" VerticalAlignment="Stretch"
Padding="{Binding ElementName=UnitBlock, Path=ActualWidth, Converter={StaticResource WidthToPaddingConverter}}"
Text="{Binding Path=Text, Mode=TwoWay}" TextChanged="TextBox_TextChanged"/>
<TextBlock x:Name="UnitBlock" Text="{Binding Path=Unit}" Margin="0,0,4,4" FontSize="10"
HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
</Grid>
</UserControl>

View File

@ -0,0 +1,31 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace Elwig.Controls {
public partial class UnitTextBox : UserControl {
public event TextChangedEventHandler? TextChanged;
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(UnitTextBox));
public string Text {
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public static readonly DependencyProperty UnitProperty = DependencyProperty.Register("Unit", typeof(string), typeof(UnitTextBox));
public string Unit {
get => (string)GetValue(UnitProperty);
set => SetValue(UnitProperty, value);
}
public UnitTextBox() {
InitializeComponent();
DataContext = this;
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs evt) {
if (TextChanged != null) TextChanged(sender, evt);
}
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Windows;
using System.Windows.Data;
using System.Globalization;
namespace Elwig.Controls {
public class WidthToPaddingConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
return new Thickness(2, 2, 4 + (double)value, 2);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
return ((Thickness)value).Right - 4;
}
}
}