I want to create a TextBox that can take measurement and convert it to different units if necessary (the end result being of type double). The conversion will be controlled by a value IsMetric. If IsMetric == true then "36.5 in" would turn into 927.1 (a double representing millimeters). Conversely, if IsMetric == false then "927.1 mm" would turn into 36.5.
I thought to use an IValueConverter on a regular TextBox, but the ConverterParameter is not a DependencyProperty and therefore I can't bind IsMetric to it.
I tried IMultiValueConverter but the ConvertBack function only receives the current value of the TextBox and not all the bound values. This means I don't know IsMetric when converting the user input.
Have I missed something with the ConvertBack function? If not, then do I need to create a class derived from TextBox?
You could use two converters one to convert from Metric and another to Metric:
public class ToMetricConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return "(metric) value";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class FromMetricConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return "(Inch) value";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
And use a DataTrigger in the UI to select the appropriate converter based on that bool value:
<Window.Resources>
<wpfApplication13:ToMetricConverter x:Key="ToMetricConverter"/>
<wpfApplication13:FromMetricConverter x:Key="FromMetricConverter"/>
</Window.Resources>
<Grid>
<StackPanel>
<CheckBox IsChecked="{Binding IsMetric,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></CheckBox>
<TextBox >
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding IsMetric,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Value="True">
<Setter Property="Text" Value="{Binding Val,Converter={StaticResource ToMetricConverter}}"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding IsMetric,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Value="False">
<Setter Property="Text" Value="{Binding Val,Converter={StaticResource FromMetricConverter}}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</StackPanel>
</Grid>
If thats the only thing you want to do, try other way to use converter parameter.
But, and i would have choose this option - if your textbox has more logics in it, or tend to have more dependecie - Create custom control that inherits from textbox, and add your own dependecy properties. Then you can use your IsMetric and convert it as you want on propertychanged etc.
I ended up with something along these lines for now. Would still enjoy a solution that doesn't require a DataTrigger for every possible value.
It's a bit different than the answer posted by #SamTheDev but along the same lines.
xaml
<UserControl x:Class="MyNamespace.Controls.MeasurementTextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:MyNamespace.Converters"
xmlns:b="clr-namespace:MyNamespace.Behaviors"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
x:Name="root">
<UserControl.Resources>
<c:MeasurementUnitConverter x:Key="muc"/>
<c:MeasurementConverter2 x:Key="mc"/>
<sys:Boolean x:Key="BooleanFalse">False</sys:Boolean>
<sys:Boolean x:Key="BooleanTrue">True</sys:Boolean>
</UserControl.Resources>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="30"/>
</Grid.ColumnDefinitions>
<TextBox Margin="0" VerticalContentAlignment="Center" HorizontalAlignment="Stretch" HorizontalContentAlignment="Right" VerticalAlignment="Stretch"
b:AutoSelectBehavior.AutoSelect="True">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding UseMetric, ElementName=root}" Value="True">
<Setter Property="Text" Value="{Binding Measurement, Mode=TwoWay, ElementName=root, Converter={StaticResource mc}, ConverterParameter={StaticResource BooleanTrue}}"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding UseMetric, ElementName=root}" Value="False">
<Setter Property="Text" Value="{Binding Measurement, Mode=TwoWay, ElementName=root, Converter={StaticResource mc}, ConverterParameter={StaticResource BooleanFalse}}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<!-- in or mm label -->
<Label VerticalAlignment="Center" Padding="0" Margin="5" HorizontalAlignment="Left" Grid.Column="1"
Content="{Binding UseMetric, ElementName=root, Converter={StaticResource muc}}"/>
</Grid>
</UserControl>
xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
namespace MyNamespace.Controls
{
/// <summary>
/// Interaction logic for MeasurementTextBox.xaml
/// </summary>
public partial class MeasurementTextBox : UserControl
{
public MeasurementTextBox()
{
// This call is required by the designer.
InitializeComponent();
}
public bool UseMetric {
get { return Convert.ToBoolean(GetValue(UseMetricProperty)); }
set { SetValue(UseMetricProperty, value); }
}
public static readonly DependencyProperty UseMetricProperty = DependencyProperty.Register("UseMetric", typeof(bool), typeof(MeasurementTextBox), new PropertyMetadata(MeasurementTextBox.UseMetricChanged));
private static void UseMetricChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
public double Measurement {
get { return (double)GetValue(MeasurementProperty); }
set { SetValue(MeasurementProperty, value); }
}
public static readonly DependencyProperty MeasurementProperty = DependencyProperty.Register("Measurement", typeof(double), typeof(MeasurementTextBox), new PropertyMetadata(MeasurementTextBox.MeasurementPropertyChanged));
private static void MeasurementPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
}
}
Converter
using System;
using System.Windows;
using System.Windows.Data;
namespace MyNamespace.Converters
{
class MeasurementConverter : IValueConverter
{
const double MILLIMETERS_IN_ONE_INCH = 25.4;
const string INCHES_ABBREVIATION = "in";
const string MILLIMETERS_ABBREVIATION = "mm";
const double ONE_THIRTY_SECOND = 0.03125;
const double ONE_SIXTEENTH = 0.0625;
const double ONE_EIGHTH = 0.125;
const double ONE_FOURTH = 0.25;
const double ONE_HALF = 0.5;
const double ONE = 1;
public double RoundToNearest(double value, int unitPrecision)
{
double fraction = 0;
int reciprocal = 0;
switch (unitPrecision)
{
case 0:
fraction = ONE;
reciprocal = (int)ONE;
break;
case 1:
fraction = ONE;
reciprocal = (int)ONE;
break;
case 2:
fraction = ONE_HALF;
reciprocal = (int)(1 / ONE_HALF);
break;
case 3:
fraction = ONE_FOURTH;
reciprocal = (int)(1 / ONE_FOURTH);
break;
case 4:
fraction = ONE_EIGHTH;
reciprocal = (int)(1 / ONE_EIGHTH);
break;
case 5:
fraction = ONE_SIXTEENTH;
reciprocal = (int)(1 / ONE_SIXTEENTH);
break;
case 6:
fraction = ONE_THIRTY_SECOND;
reciprocal = (int)(1 / ONE_THIRTY_SECOND);
break;
}
return Math.Round(value * reciprocal, MidpointRounding.AwayFromZero) * fraction;
}
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)
{
string strValue = (string)value;
bool isMetric = (bool)parameter;
double enteredValue = 0;
bool enteredValueIsImperial = false;
if (strValue.EndsWith(INCHES_ABBREVIATION))
{
enteredValueIsImperial = true;
strValue = strValue.Substring(0, strValue.Length - INCHES_ABBREVIATION.Length);
}
else if (strValue.EndsWith(MILLIMETERS_ABBREVIATION))
{
enteredValueIsImperial = false;
strValue = strValue.Substring(0, strValue.Length - MILLIMETERS_ABBREVIATION.Length);
}
else if (isMetric)
{
enteredValueIsImperial = false;
}
else
{
enteredValueIsImperial = true;
}
try
{
enteredValue = double.Parse(strValue);
}
catch (FormatException)
{
return DependencyProperty.UnsetValue;
}
if (isMetric)
{
if (enteredValueIsImperial)
{
//inches to mm
return RoundToNearest(enteredValue * MILLIMETERS_IN_ONE_INCH, 0);
//0 is mm
}
else
{
//mm to mm
return RoundToNearest(enteredValue, 0);
//0 is mm
}
}
else
{
if (enteredValueIsImperial)
{
//inches to inches
return RoundToNearest(enteredValue, 5);
}
else
{
//mm to inches
return RoundToNearest(enteredValue / MILLIMETERS_IN_ONE_INCH, 5);
}
}
}
}
}
Usage:
<mynamespace:MeasurementTextBox Measurement="{Binding SomeLength, Mode=TwoWay}"
UseMetric="{Binding IsMetric}"/>
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 have a custom ComboBox with this definition.
<UserControl ...>
<Grid>
<Grid.Resources>
<local:InheritanceEnumValueConverter x:Key="inheritanceEnumValueConverter"/>
</Grid.Resources>
<samples:MaterialEnumComboBox x:Name="materialComboBoxGenerateStatements"
Grid.Column="3" Grid.Row="1" Height="30" Width="120"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
IsEditable="True" IsReadOnly="True"
EnumSource="enums:YesNoInherit" Margin="52,0">
<samples:MaterialEnumComboBox.Style>
<Style TargetType="samples:MaterialEnumComboBox">
<Setter Property="Text"
Value="{Binding Path=selectedModel.GenerateStatements,
UpdateSourceTrigger=PropertyChanged}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding selectedModel.GenerateStatements}">
<DataTrigger.Value>
<enums:YesNoInherit>Inherit</enums:YesNoInherit>
</DataTrigger.Value>
<Setter Property="Text">
<Setter.Value>
<MultiBinding Converter="{StaticResource inheritanceEnumValueConverter}">
<Binding Path="selectedModel.GenerateStatements"></Binding>
<Binding Path="selectedModel.DefaultGenerateStatements"></Binding>
</MultiBinding>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</samples:MaterialEnumComboBox.Style>
</samples:MaterialEnumComboBox>
</Grid>
</UserControl>
Here is the custom ComboBox. There is no significant XAML.
public partial class MaterialEnumComboBox
{
private Type _enumSource;
public Type EnumSource
{
get { return _enumSource; }
set
{
_enumSource = value;
this.SetBinding(ItemsSourceProperty, new Binding { Source = Enum.GetValues(_enumSource) });
}
}
public MaterialEnumComboBox()
{
InitializeComponent();
}
}
Here is the converter for the MultiBinding.
public class InheritanceEnumValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.All(x => x.GetType().BaseType != typeof(Enum)))
throw new NotSupportedException();
else
{
Enum typedValue = values[0] as Enum;
Enum typedValue2 = values[1] as Enum;
string strAbbr = typedValue.Description(); // based on the Description attribute for the enum.
if (typedValue != null)
{
if (strAbbr == "Inherited")
strAbbr = typedValue2.ToString() + " " + "(" + "Inherited" + ")";
return strAbbr;
}
else
return string.Empty;
}
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
Type enumTargetType = targetTypes.First();
var enumValue = Enum.Parse(enumTargetType, value.ToString());
return new object[] { enumValue };
}
}
Here is the model. The ObservableObject is from Galasoft.MvvmLight.
public class MyModel: ObservableObject
{
private YesNoInherit _generateStatements;
private YesNoInherit _defaultGenerateStatements;
public YesNoInherit GenerateStatements
{
get { return _generateStatements; }
set
{
if (value != _generateStatements)
{
_generateStatements = value;
RaisePropertyChanged("GenerateStatements");
}
}
}
public YesNoInherit DefaultGenerateStatements
{
get { return _defaultGenerateStatements; }
set
{
if (value != _defaultGenerateStatements)
{
_defaultGenerateStatements = value;
RaisePropertyChanged("DefaultGenerateStatements");
}
}
}
}
Finally the enum.
public enum YesNoInherit
{
[Description("No")]
No = 0, ///<
[Description("Yes")]
Yes = 1, ///<
[Description("Inherited")]
Inherit = 2
}
The "DefaultGenerateStatements" property is always "Yes" for this example.
When the combo box first appears, the value is "Yes".
I change the value of the combo box in this order.
When I change from "Yes" to "No", the converter is not executed. I expect this.
When I change from "No" to "Yes", the converter is not executed. I expect this.
When I change from "Yes" to "Inherit", the converter executes, gets the value of DefaultGenerateStatements, and returns the text "Yes (Inherited)" to the combo box. I expect this.
When I change from "Inherit" to "Yes", the text "Yes" appears in the combo box as it should, but the converter is executed. I'm not sure if the converter should execute.
When I change from "Yes" to "No", the text "No" appears as it should, but the converter is executed. I don't believe the converter should execute.
After this, any change results in the converter being executed.
For steps 4 and 5, the DataTrigger fires when I don't think it should. Is there something wrong with my code?
I want when visibility of "StckPnl1" is set to Collapsed, my property "IsBusyIndicatorShowing" of "BusyDeco1" is being set to true.
What's the simplest solution?
<ctrls:BusyDecorator x:Name="BusyDeco1" IsBusyIndicatorShowing="??" Style="{DynamicResource BusyDecorator1}"/>
<StackPanel x:Name="StckPnl1" Visibility="Collapsed">
Use a DataTrigger:
<ctrls:BusyDecorator>
<ctrls:BusyDecorator.Style>
<Style TargetType="{x:Type ctrls:BusyDecorator}" BasedOn="{DynamicResource BusyDecorator1}">
<Setter Property="IsBusyIndicatorShowing" Value="False" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=StckPnl1, Path=Visibility}" Value="Collapsed">
<Setter Property="IsBusyIndicatorShowing" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
</ctrls:BusyDecorator.Style>
</ctrls:BusyDecorator>
i use a converter for this stuff.
<ctrls:BusyDecorator x:Name="BusyDeco1"
IsBusyIndicatorShowing="{Binding ElementName=StckPnl1, Path=Visibility, Converter={StaticResource TrueIfNotVisibleConverter}}" Style="{DynamicResource BusyDecorator1}"/>
<StackPanel x:Name="StckPnl1" Visibility="Collapsed">
<Converter:BoolToVisibilityConverter x:Key="TrueIfVisibleConverter" Inverted="False" Not="False" />
<Converter:BoolToVisibilityConverter x:Key="TrueIfNotVisibleConverter" Inverted="False" Not="True" />
<Converter:BoolToVisibilityConverter x:Key="VisibleIfTrueConverter" Inverted="True" Not="False" />
<Converter:BoolToVisibilityConverter x:Key="VisibleIfNotTrueConverter" Inverted="True" Not="True" />
public class BoolToVisibilityConverter : IValueConverter
{
private bool inverted = false;
private bool not = false;
public bool Inverted
{
get { return inverted; }
set { inverted = value; }
}
public bool Not
{
get { return not; }
set { not = value; }
}
private object VisibilityToBool(object value)
{
if (!(value is Visibility))
return DependencyProperty.UnsetValue;
return (((Visibility)value) == Visibility.Visible) ^ Not;
}
private object BoolToVisibility(object value, object parameter)
{
if (!(value is bool))
return DependencyProperty.UnsetValue;
var hiddenodercollapsed = Visibility.Collapsed;
if (parameter != null && parameter.ToString().ToLower().Contains("hidden"))
hiddenodercollapsed = Visibility.Hidden;
return ((bool)value ^ Not) ? Visibility.Visible : hiddenodercollapsed;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Inverted ? BoolToVisibility(value, parameter) : VisibilityToBool(value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Inverted ? VisibilityToBool(value) : BoolToVisibility(value, parameter);
}
}
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 };
}
}
I'm trying to bind the background of a Tile in a RadTileList, Tiles are created from a collection on the Itemsource of the RadTileList, so far I've tried changing the background on the border container in the Datatemplate, but it seems that the Tile background property is wining over that.
In the code above , I've tried to set the ItemContainerStyle and set the binding for the background, but nothing changes, I hope someone could help me.
Note: The color of the background is a string var so im using a converter, wich I tested independently
<telerik:RadTileList ItemsSource="{Binding Modulo.Modulos_Detail}" TileReorderMode="None"
ScrollViewer.HorizontalScrollBarVisibility="Visible">
<telerik:RadTileList.ItemContainerStyle>
<Style >
<Setter Property="telerik:Tile.TileType" Value="Quadruple" />
<Setter Property="telerik:Tile.Background" Value="{Binding .Color, Converter={StaticResource strHexColorConverter}}" />
</Style>
</telerik:RadTileList.ItemContainerStyle>
<telerik:RadTileList.ItemTemplate>
<DataTemplate>
<Border >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Grid.Row="0"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Source="{Binding .Imagenes.Imagen}" Stretch="Uniform"></Image>
<TextBlock Grid.Row="1" Padding="5"
Text="{Binding Descripcion}" FontSize="20" TextWrapping="Wrap"
VerticalAlignment="Bottom"/>
<!--<Image Source="{Binding .LockViewImage, Converter={StaticResource imgBitmapImageConverter}}" />-->
</Grid>
</Border>
</DataTemplate>
</telerik:RadTileList.ItemTemplate>
Create a seperate style with the Tile as target type. Then you only need to set the Background property. Optionally you could give the style a name and set it explicitly on the RadTileList.
<UserControl.Resources>
<ResourceDictionary>
<Style TargetType="telerik:Tile" BasedOn="{StaticResource TileStyle}">
<Setter Property="Background" Value="{StaticResource OmegaMainColorBrush}" />
</Style>
</ResourceDictionary>
</UserControl.Resources>
In my opinion you can use a Boolean Attached property in the telerik:Tile control style. If that property is True then you create the binding in code behind. The only one thing you should care about is that the Content of the Tile will hold the the object that the .Color is defined there.
Here is the code.
Style (put this into the Resource part)
<Style TargetType="telerik:Tile" BasedOn="{StaticResource {x:Type telerik:Tile}}">
<Setter Property="flowConfiguration:TileAttachedProperties.IsTyleTypeBound" Value="True"/>
</Setter>
The attached property code(with converters)
public class TileAttachedProperties
{
public static readonly DependencyProperty IsTyleTypeBoundProperty = DependencyProperty.RegisterAttached(
"IsTyleTypeBound",
typeof (bool),
typeof (TileAttachedProperties),
new PropertyMetadata(default(bool), IsTyleBoundPropertyChangedCallback));
private static void IsTyleBoundPropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
var tile = sender as Tile;
var isBound = (bool) args.NewValue;
if(tile == null || isBound == false) return;
tile.Loaded += TileOnLoaded;
}
private static void TileOnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
var tile = sender as Tile;
if (tile == null) return;
tile.Loaded -= TileOnLoaded;
var tileContent = tile.Content;
if (tileContent == null || tileContent is ItemTypeWrapper == false) return;
//here we create binding to define if the type of the Tile(single or double)
var tileTypeBinding = new Binding("IsDouble");
tileTypeBinding.Source = tileContent;
tileTypeBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
tileTypeBinding.Converter = new Bool2TileTypeConverter();
tile.SetBinding(Tile.TileTypeProperty, tileTypeBinding);
//here we create binding to define the background of the tile
var tileBackgroundBinding = new Binding("IsDouble");
tileBackgroundBinding.Source = tileContent;
tileBackgroundBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
tileBackgroundBinding.Converter = new Bool2BackgroundConverter();
tile.SetBinding(Tile.BackgroundProperty, tileBackgroundBinding);
}
public static void SetIsTyleTypeBound(DependencyObject element, bool value)
{
element.SetValue(IsTyleTypeBoundProperty, value);
}
public static bool GetIsTyleTypeBound(DependencyObject element)
{
return (bool) element.GetValue(IsTyleTypeBoundProperty);
}
}
internal class Bool2BackgroundConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var isDouble = (bool)value;
return isDouble ? new SolidColorBrush(Color.FromArgb(255, 255, 0, 255)) : new SolidColorBrush(Color.FromArgb(255,0, 255, 255));
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
internal class Bool2TileTypeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var isDouble = (bool) value;
return isDouble ? TileType.Double : TileType.Single;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
How it looks like:
Regards,