In our WPF project, we are using a TextBox to let the user enter some length measures. This measures may be in millimeters, centimeters, inches, etc (depending on the user configuration).
We need to show the numbers on the TextBox along with the current measurement unit, like:
"120 mm"
"16 cm"
"1' 2 3/4"
and so on.
I am using ValueConverter to both perform the unit converting and to add the extra formatting. The UpdateSourceTrigger is default, so the binding applies when the user leaves the control.
The challenge here is that while editing the value, these "mm" or "cm" are not shown. So the previous examples in edit mode would be:
"120"
"16"
"1 2 3/4"
How can I make that happen?
Here is the Converter code for some cenários:
/// <summary>
/// Converts and formats the value (in milimeters) to the required unit/format
/// </summary>
public class LengthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
LengthUnit unit = MainWindow.CurrentLengthUnit;
switch (unit.Id)
{
case 1: //Milimeters
return value + " mm";
case 2: //Centimeters
return (double)value / 10.0 + " cm";
default:
throw new InvalidOperationException("Unknown length unit.");
}
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
string rawValue = value as string;
LengthUnit unit = MainWindow.CurrentLengthUnit;
switch (unit.Id)
{
case 1: //Milimeters
return rawValue.Replace("mm", "").Trim();
case 2: //Centimeters
return Double.Parse(rawValue.Replace("cm", "").Trim()) * 10.0;
default:
throw new InvalidOperationException("Unknown length unit.");
}
}
}
What about this:
view:
<TextBox>
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsFocused" Value="False">
<Setter Property="Text">
<Setter.Value>
<MultiBinding Converter="{StaticResource LengthConverter}">
<Binding Path="Foo"/>
<Binding Path="Text" RelativeSource="{RelativeSource Mode=Self}"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Text" Value="{Binding Foo}"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
converter:
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Count() > 1)
{
var value = values[1].ToString();
if (value != String.Empty)
{
int output;
Int32.TryParse(value, out output);
if (output != (int)values[0])
return value;
}
}
return String.Format("{0} {1}", values[0], "mm");
}
So far I managed to fix this problem using only MultiBinding and passing the TextBox control as a parameter (not very nice I suppose).
On the converter code, I check for the control property "IsFocused" and return values the way I need. I am not able to pass the "IsFocused" property directly on the binding because it is readonly while the TextBox is two way bound to the source.
The following code shows some additional handlers used to "force" binding when the controls gain or loose focus.
<TextBox x:Name="leadInTextBox"
LostFocus="SizeLostFocus"
MouseDoubleClick="SelectText"
GotKeyboardFocus="SelectText"
PreviewMouseLeftButtonDown="SelectivelyMouseButton"
PreviewTextInput="PreviewTextInput">
<TextBox.Text>
<MultiBinding Mode="TwoWay" Converter="{StaticResource MultiLengthConverter}" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True">
<Binding Source="{x:Static local:ApplicationService.UserInfo}" Path="LeadIn" StringFormat="F2" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
<Binding RelativeSource="{RelativeSource Self}" Path="." />
</MultiBinding>
</TextBox.Text>
</TextBox>
converter:
public class MultiLengthConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
LengthUnit currentUnit = ApplicationService.UserInfo.LengthUnit;
TextBox txtBox = values[1] as TextBox;
switch (currentUnit)
{
case LengthUnit.Millimeter:
return String.Format("{0:N2}{1}", values[0], txtBox.IsFocused == true ? string.Empty : " mm");
case LengthUnit.Centimeter:
return String.Format("{0:N2}{1}", (double)values[0] / 10.0, txtBox.IsFocused == true ? string.Empty : " cm");
case LengthUnit.DecimalInch:
return String.Format("{0:N2}{1}", (double)values[0] / 25.4, txtBox.IsFocused == true ? string.Empty : " in");
default:
throw new InvalidOperationException("Unknown length unit.");
}
}
public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
{
string rawValue = value as string;
LengthUnit currentUnit = ApplicationService.UserInfo.LengthUnit;
object ret;
switch (currentUnit)
{
case LengthUnit.Millimeter:
ret = Double.Parse(rawValue.Replace("mm", "").Trim());
break;
case LengthUnit.Centimeter:
ret = Double.Parse(rawValue.Replace("cm", "").Trim()) * 10.0;
break;
case LengthUnit.DecimalInch:
ret = Double.Parse(rawValue.Replace("in", "").Trim()) * 25.4;
break;
default:
throw new InvalidOperationException("Unknown length unit.");
}
return new object[] { ret, null };
}
}
Related
I want to disable some items of combobox by specific conditions. For this issue, I used multibinding.
If I described all items of combobox in xaml, there is no problem. But I want to populate combobox items programmatically. So in this case , I could not get items, returns null, and throw me out of the program in the first step.
my xaml codes are like that:
<Window.Resources>
<local:TekerDisabler x:Key="tekerDisabler"/>
</Window.Resources>
<Grid>
<ComboBox x:Name="cbx" HorizontalAlignment="Left" Margin="41,125,0,0" VerticalAlignment="Top" Width="227">
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="IsEnabled">
<Setter.Value>
<MultiBinding Converter="{StaticResource tekerDisabler}">
<Binding ElementName="txt1" Path="Text"/>
<Binding ElementName="txt2" Path="Text"/>
<Binding ElementName="txt3" Path="Text"/>
<Binding RelativeSource="{RelativeSource Self}"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
<TextBox x:Name="txt1" HorizontalAlignment="Left" Height="23" Margin="41,38,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="txt2" HorizontalAlignment="Left" Height="23" Margin="207,38,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="211" TextChanged="txt2_TextChanged"/>
<TextBox x:Name="txt3" HorizontalAlignment="Left" Height="24" Margin="478,37,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="196"/>
</Grid>
and my c# codes are like that:
namespace App1.Pencereler
{
public partial class deneme : Window
{
public deneme()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
cbx.Items.Add("0");
cbx.Items.Add("1");
cbx.Items.Add("2");
cbx.Items.Add("3");
}
private void txt2_TextChanged(object sender, TextChangedEventArgs e)
{
cbx.SelectedIndex = 1;
}
}
class TekerDisabler : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
bool enable = true;
var itemler = values[3] as ComboBoxItem;
if (itemler == null || values[0].ToString() == null || values[1].ToString() == null || values[2].ToString() == null)
{ enable = true; }
else
{
switch (values[0].ToString())
{
case "a":
switch (values[1].ToString())
{
case "b":
switch (values[2].ToString())
{
case "c":
switch (itemler.Content.ToString())
{
case "0":
case "2":
enable = false;
break;
default:
enable = true;
break;
}
break;
default:
enable = true;
break;
}
break;
default:
enable = true;
break;
}
break;
default:
enable = true;
break;
}
}
return enable;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
For Example, in the first step, I write txt1:a, txt2:b, txt3:d and so all items' display enabled, and then I write txt1:a, txt2:b, txt3:c and contents of combobox (0,2) disabled, there is no problem. But when running program, in the first step I write txt1:a, txt2:b, txt3:c when drop down combobox, program trow me out.
How to overcome this problem?
Error Message and details are like that:
And Error details are like that:
System.NullReferenceException
HResult=0x80004003
İleti=Nesne başvurusu bir nesnenin örneğine ayarlanmadı.
Kaynak=App1
StackTrace:
konum App1.Pencereler.TekerDisabler.Convert(Object[] values, Type targetType, Object parameter, CultureInfo culture) D:\C Sharp\WPF\App1\App1\Pencereler\deneme.xaml.cs içinde: 60. satır
konum System.Windows.Data.MultiBindingExpression.TransferValue()
konum System.Windows.Data.MultiBindingExpression.Transfer()
konum System.Windows.Data.MultiBindingExpression.UpdateTarget(Boolean includeInnerBindings)
konum System.Windows.Data.MultiBindingExpression.AttachToContext(Boolean lastChance)
konum System.Windows.Data.MultiBindingExpression.AttachOverride(DependencyObject d, DependencyProperty dp)
konum System.Windows.Data.BindingExpressionBase.OnAttach(DependencyObject d, DependencyProperty dp)
It would be interesting to know what exact error you get.
I assume the ComboBoxItem.Content returns null. The item containers are rendered (generated) after the ComboBox is opened. Only the data items exist at this moment. So opening the drop down the first time all item containers are null and about to be rendered.
Anyway, the following simplified version of your code will very likely fix your problem:
TekerDisabler.cs
public class TekerDisabler : IMultiValueConverter
{
#region Implementation of IMultiValueConverter
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var currentItem = values[0] as string;
var predicate = "abc";
string input = string.Concat(values.Skip(1).Cast<string>());
return !(input.Equals(predicate, StringComparison.Ordinal)
&& (currentItem.Equals("0", StringComparison.Ordinal)
|| currentItem.Equals("2", StringComparison.Ordinal)));
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) =>
throw new NotSupportedException();
#endregion
}
TekerDisabler.cs - Alternative version
public class TekerDisabler : IMultiValueConverter
{
#region Implementation of IMultiValueConverter
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var currentItem = values[0] as string;
var predicate = "abc";
string input = string.Concat(values.Skip(1).Cast<string>());
return !(values[1].Equals("a", StringComparison.Ordinal)
&& values[2].Equals("b", StringComparison.Ordinal)
&& values[3].Equals("c", StringComparison.Ordinal)
&& (currentItem.Equals("0", StringComparison.Ordinal)
|| currentItem.Equals("2", StringComparison.Ordinal)));
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) =>
throw new NotSupportedException();
#endregion
}
MainWindow.xamlk.cs
partial class MainWIndow : Window
{
public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register(
"Items",
typeof(ObservableCollection<string>),
typeof(MainWindow),
new PropertyMetadata(default(ObservableCollection<string>)));
public ObservableCollection<string> Items
{
get => (ObservableCollection<string>) GetValue(MainWindow.ResultsProperty);
set => SetValue(MainWindow.ResultsProperty, value);
}
}
MainWindow.xaml
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=main:MainWindow}, Path=Items}">
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="IsEnabled">
<Setter.Value>
<MultiBinding Converter="{StaticResource CellForegroundMultiValueConverter}">
<Binding />
<Binding ElementName="TextBox1" Path="Text" />
<Binding ElementName="TextBox2" Path="Text" />
<Binding ElementName="TextBox3" Path="Text" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
<TextBox x:Name="TextBox1" />
<TextBox x:Name="TextBox2" />
<TextBox x:Name="TextBox3" />
I accomlished to overcome my problem by adding
cbx.IsDropDownOpen = true;
cbx.IsDropDownOpen = false;
after
cbx.Items.Add("0");
cbx.Items.Add("1");
cbx.Items.Add("2");
cbx.Items.Add("3");
in 'Window_Loaded' event.
I am trying to use a single IMultiValueConverter for more than one controls in XAML.
I am using a simple string Literal to tell what value the IMultiValueConverter is supposed to return.
But I am getting DependencyProperty.UnsetValue in values[2],ie value of parametter named Command when it comes to convert function of ModifierCategoryEnableDisable.
Similar arrangment is working on similar controls on this XAML form within other IMultiValueConverters but not here.
Please guide what am i missing?
NOTE:
CurrentRec is the currently selected object from the ViewModel
DM_CategoryData is a Class and Current_Selected_Category is a List<DM_CategoryData> in the ViewModel's current object,ie CurrenRec.
XAML:
<GroupBox Width="226" Height="117" Margin="0" Canvas.Top="252" Header="Modifiers" Canvas.Left="55" >
<GroupBox.IsEnabled>
<MultiBinding Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" Converter="{StaticResource MDNS}">
<Binding Path="SearchFound" />
<Binding Path="CurrentRec.Current_Selected_Category"/>
<Binding Path="Command" FallbackValue="1" />
</MultiBinding>
</GroupBox.IsEnabled>
</GroupBox>
C#:
public class ModifierCategoryEnableDisable : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
string Command = values[2].ToString();
bool Retval1 = false;
string Retval2 = "";
switch(Command)
{
case "1":
bool SearchFound = (bool)values[0];
DM_CategoryData CurrentSelectedItemCategory = (DM_CategoryData)(values[1]);
Retval1 = SearchFound && (CurrentSelectedItemCategory == null ? true : CurrentSelectedItemCategory.IsModifier.Equals("1") ? false : true);
break;
case "2":
Retval2 = "0";
break;
}
if(Command.Equals("1"))
{
return Retval1;
}
else
{
return Retval2;
}
}
}
In order to provide additional static data to a multibinding converter, use a ConverterParameter:
<MultiBinding Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" Converter="{StaticResource MDNS}" ConverterParameter="1">
<Binding Path="SearchFound" />
<Binding Path="CurrentRec.Current_Selected_Category"/>
</MultiBinding>
And check the parameter in the Convert method:
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
string Command = parameter as string;
// ...
}
You are trying to set the fallbackvalue for GroupBox.IsEnabled property and it is a bool type. But you are setting the value as 1. So only Values[2] returns the UnsetValue. Try to set bool value as Fallbackvalue.
I have a MultiBinding that is not working on TextBox.Text. I have the same code that is binding properly to Value of Extended WPF Toolkit's IntegerUpDown.
It is going through an IMultiValueConverter that takes the bound POCO and the listbox it is part of (it is displaying the order of the item in the listbox)
Here is the code:
<!--works-->
<wpf:IntegerUpDown ValueChanged="orderChanged" x:Name="tripOrder">
<wpf:IntegerUpDown.Value>
<MultiBinding Converter="{StaticResource listBoxIndexConverter}" Mode="OneWay">
<Binding />
<Binding ElementName="listTrips" />
</MultiBinding>
</wpf:IntegerUpDown.Value>
</wpf:IntegerUpDown>
<!--doesn't work-->
<TextBox x:Name="tripOrder2">
<TextBox.Text>
<MultiBinding Converter="{StaticResource listBoxIndexConverter}" Mode="OneWay">
<Binding />
<Binding ElementName="listTrips" />
</MultiBinding>
</TextBox.Text>
</TextBox>
Here is the result:
I don't believe it is relevant, but just in case, here is the class that performs the conversion:
public class ListBoxIndexConverter : IMultiValueConverter
{
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var trip = values[0] as TripBase;
if (trip == null)
{
return null;
}
var lb = values[1] as CheckListBox;
if (lb == null)
{
return null;
}
//make it 1 based
return lb.Items.IndexOf(trip) + 1;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
The converter should return the type that the property expects. The reason is that in regular use of the properties (i.e. without Binding), the properties may have type converters that convert from one type (or more) to the type required by the property. For example, when you write:
<ColumnDefinition Width="Auto"/>
there's a converter that converts string "Auto" to:
new GridLength(1, GridUnitType.Auto)
When using binding, this mechanism is bypassed since the converter should return the right type.
So, to fix your issue, at the return of your converter:
return (lb.Items.IndexOf(trip) + 1).ToString();
This should fix the TextBox.
Now, for the IntegerUpDown. It sounds like it actually expects to receive an int and returning a string will break it. So, again, change the return of the converter:
if (targetType == typeof(int))
{
return lb.Items.IndexOf(trip) + 1;
}
else if (targetType == typeof(string))
{
return (lb.Items.IndexOf(trip) + 1).ToString();
}
else
{
throw new NotImplementedException(String.Format("Can not convert to type {0}", targetType.ToString()));
}
The binding is not going to work, because the listTrips is not changing when the list box's selected value changes. The thing that changes is listTrips.SelectedItem, so you should bind against it:
<Binding Path="SelectedItem" ElementName="listTrips"/>
Actually, I wonder why it works for the first example.
I have a textbox that is bound to a class with a property of type Timespan, and have written a value converter to convert a string into TimeSpan.
If a non number is entered into the textbox, I would like a custom error message to be displayed (rather than the default 'input string is in the wrong format').
The converter code is:
public object ConvertBack(
object value,
Type targetType,
object parameter,
CultureInfo culture)
{
try
{
int minutes = System.Convert.ToInt32(value);
return new TimeSpan(0, minutes, 0);
}
catch
{
throw new FormatException("Please enter a number");
}
}
I have set 'ValidatesOnExceptions=True' in the XAML binding.
However, I have come across the following MSDN article, which explains why the above will not work:
"The data binding engine does not catch exceptions that are thrown by a user-supplied converter. Any exception that is thrown by the Convert method, or any uncaught exceptions that are thrown by methods that the Convert method calls, are treated as run-time errors"
I have read that 'ValidatesOnExceptions does catch exceptions in TypeConverters, so my specific questions are:
When would you use a TypeConverter over a ValueConverter
Assuming a TypeConverter isn't the answer to the issue above, how can I display my custom error message in the UI
I would use a ValidationRule for that, this way the converter can be sure that the conversion works since it only is called if validation succeeds and you can make use of the attached property Validation.Errors which will contain the errors your ValidationRule creates if the input is not the way you want it.
e.g. (note the tooltip binding)
<TextBox>
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="Pink"/>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
<TextBox.Text>
<Binding Path="Uri">
<Binding.ValidationRules>
<vr:UriValidationRule />
</Binding.ValidationRules>
<Binding.Converter>
<vc:UriToStringConverter />
</Binding.Converter>
</Binding>
</TextBox.Text>
</TextBox>
I used validation and converter to accept null and numbers
XAML:
<TextBox x:Name="HeightTextBox" Validation.Error="Validation_Error">
<TextBox.Text>
<Binding Path="Height"
UpdateSourceTrigger="PropertyChanged"
ValidatesOnDataErrors="True"
NotifyOnValidationError="True"
Converter="{StaticResource NullableValueConverter}">
<Binding.ValidationRules>
<v:NumericFieldValidation />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Code Behind:
private void Validation_Error(object sender, ValidationErrorEventArgs e)
{
if (e.Action == ValidationErrorEventAction.Added)
_noOfErrorsOnScreen++;
else
_noOfErrorsOnScreen--;
}
private void Confirm_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = _noOfErrorsOnScreen == 0;
e.Handled = true;
}
ValidationRule :
public class NumericFieldValidation : ValidationRule
{
private const string InvalidInput = "Please enter valid number!";
// Implementing the abstract method in the Validation Rule class
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
float val;
if (!string.IsNullOrEmpty((string)value))
{
// Validates weather Non numeric values are entered as the Age
if (!float.TryParse(value.ToString(), out val))
{
return new ValidationResult(false, InvalidInput);
}
}
return new ValidationResult(true, null);
}
}
Converter :
public class NullableValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (string.IsNullOrEmpty(value.ToString()))
return null;
return value;
}
}
You shouldn't throw exceptions from the converter. I would implement IDataErrorInfo and implement the Error and String on that. Please check https://web.archive.org/web/20110528131712/http://www.codegod.biz/WebAppCodeGod/WPF-IDataErrorInfo-and-Databinding-AID416.aspx.
HTH daniell
I want to activate a trigger if the bound items property ID is equal to a property in my custom control.
But i can't use bindings in my triggers!
How would i do this?
This should work:
<DataTrigger
Value="True">
<DataTrigger.Binding>
<MultiBinding
Converter="{x:Static local:EqualityConverter.Instance}">
<Binding
Path="BoundProperty" />
<Binding
ElementName="MockCustomControl"
Path="Text" />
</MultiBinding>
</DataTrigger.Binding>
<DataTrigger.Setters>
<Setter
TargetName=" ... "
Property=" ... "
Value=" ... " />
...
...
</DataTrigger.Setters>
</DataTrigger>
And the converter may be something similar to this:
public class EqualityConverter : IMultiValueConverter
{
public static readonly EqualityConverter Instance = new EqualityConverter();
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (values == null || values.Length == 0) return false;
for (int i = 1; i < values.Length; i++)
if (!values[i].Equals(values[0])) return false;
return true;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
Note that I've used a generic convrter that can campare n values, but you can use a simple one as per your needs.
Hope this helps.
DataTrigger's value property doesn't take bindings?
<DataTrigger Binding="{Binding BoundPropertyId}" Value="{Binding ElementName=Mine, Path=Property}" />
If that doesn't work, you might need to hook onto an event and process it yourself in the code-behind.