FallBackValue in IMultiValueConverter is UnsetValue - wpf

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.

Related

Extracting index from Grid.Row\Column DependencyProperty

I have the following Button:
<Button Grid.Row="0" Grid.Column="0" Command="{Binding DrawXO}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource BoardIndexConverter}">
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}" Path="(Grid.Row)"></Binding>
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}" Path="(Grid.Column)" ></Binding>
</MultiBinding>
</Button.CommandParameter>
</Button>
and the following MultiValueConverter:
class BoardIndexConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values.Clone();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
How do I get the actual value of the Grid.Row\Column from a DependencyProperty in my command?:
class DrawXOCommand : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
var values = (object[])parameter;
var row = (int)(values[0]);
var column = (int)values[1];
}
public event EventHandler CanExecuteChanged;
You should have seen Binding error messages in the Output Window in Visual Studio, because the Button is not an ancestor element here.
Instead of FindAncestor you should use Self to use the Button as source object:
<MultiBinding Converter="{StaticResource BoardIndexConverter}">
<Binding RelativeSource="{RelativeSource Self}" Path="(Grid.Row)"/>
<Binding RelativeSource="{RelativeSource Self}" Path="(Grid.Column)"/>
</MultiBinding>
Your converter may also be implemented a little safer:
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length == 2 && values[0] is int && values[1] is int)
{
return new Tuple<int, int>((int)values[0], (int)values[1]);
}
return null;
}
Then check the command parameter like this:
public void Execute(object parameter)
{
var cell = parameter as Tuple<int, int>;
if (cell != null)
{
var row = cell.Item1;
var column = cell.Item2;
...
}
}

Binding a bool property to a bool expression [duplicate]

I have a WPF control that has a Message property.
I currently have this:
<dxlc:LayoutItem >
<local:Indicator Message="{Binding PropertyOne}" />
</dxlc:LayoutItem>
But i need that Message property to be bound to two properties.
Obviously can't be done like this, but this can help explain what it is I want:
<dxlc:LayoutItem >
<local:Indicator Message="{Binding PropertyOne && Binding PropertyTwo}" />
</dxlc:LayoutItem>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="FirstName"/>
<Binding Path="LastName"/>
</MultiBinding>
</TextBlock.Text>
Try use the MultiBinding:
Describes a collection of Binding objects attached to a single binding target property.
Example:
XAML
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource myNameConverter}"
ConverterParameter="FormatLastFirst">
<Binding Path="FirstName"/>
<Binding Path="LastName"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Converter
public class NameConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
string name;
switch ((string)parameter)
{
case "FormatLastFirst":
name = values[1] + ", " + values[0];
break;
case "FormatNormal":
default:
name = values[0] + " " + values[1];
break;
}
return name;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
string[] splitValues = ((string)value).Split(' ');
return splitValues;
}
}
You can't do And operation in XAML.
Create wrapper property in your view model class which will return and of two properties and bind with that property instead.
public bool UnionWrapperProperty
{
get
{
return PropertyOne && PropertyTwo;
}
}
XAML
<local:Indicator Message="{Binding UnionWrapperProperty}" />
Another approach would be to use MultiValueConverter. Pass two properties to it and return And value from the converter instead.

Can WPF Databinding source include a parameter?

Can I pass an index number into this list (SomeList)?
FontSize="{Binding FontSize, Source={x:Static ut:ViewSetupData.SomeList}, FallbackValue=12}"
You can put a constant indexer in the Path:
{Binding Path=[(sys:Int32)0], Source={x:Static ut:ViewSetupData.SomeList}}
But you can't bind a property of a Binding, so there's no way to stuff a parameter in there. However, you can combine multiple bindings in a MultiBinding, so you could use one of those with a multi-value converter:
C#:
public class IListIndexerConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
// You might want a little more error-checking than this...
return ((IList)values[0])[(int)values[1]];
}
public virtual object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return null;
}
}
XAML:
<TextBlock>
<TextBlock.Resources>
<local:IListIndexerConverter x:Key="ListIndexer" />
</TextBlock.Resources>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource ListIndexer}">
<Binding Source="{x:Static ut:ViewSetupData.SomeList}" />
<Binding
ElementName="MyComboBox"
Path="SelectedIndex"
/>
</MultiBinding>
</TextBlock.Test>
</TextBlock>
Update
While you were marking this as the solution, I was writing a more complete solution that addressed your need to grab a property from the list item:
C#:
public class ListItemPropertyGetter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
try
{
var list = values[0] as IList;
var index = (int)(values[1] ?? 0);
var propname = values[2] as String;
object item = list[index];
var prop = item.GetType().GetProperty(propname);
var propvalue = prop.GetValue(item);
return propvalue;
}
catch
{
return null;
}
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
// Gotta put these somewhere
public static List<FontSizeThing> FontSizeThings { get; } =
new List<FontSizeThing>
{
new FontSizeThing(10),
new FontSizeThing(10.5),
new FontSizeThing(11),
new FontSizeThing(12),
new FontSizeThing(14),
new FontSizeThing(15),
};
}
public class FontSizeThing {
public FontSizeThing(double n) { FontSize = n; }
public double FontSize { get; set; }
}
XAML:
<ComboBox x:Name="FontSizeOptionCombo">
<sys:Int32>0</sys:Int32>
<sys:Int32>1</sys:Int32>
<sys:Int32>2</sys:Int32>
<sys:Int32>3</sys:Int32>
<sys:Int32>4</sys:Int32>
</ComboBox>
<TextBlock Text="Testing">
<TextBlock.Resources>
<hconv:ListItemPropertyGetter x:Key="ListItemPropertyGetter" />
</TextBlock.Resources>
<TextBlock.FontSize>
<MultiBinding Converter="{StaticResource ListItemPropertyGetter}" StringFormat="{}{0}">
<Binding Source="{x:Static hconv:ListItemPropertyGetter.FontSizeThings}" />
<Binding ElementName="FontSizeOptionCombo" Path="SelectedItem" />
<Binding Source="FontSize" />
</MultiBinding>
</TextBlock.FontSize>
</TextBlock>
FINAL UPDATE
Note that if I had merely populated FontSizeOptionCombo with the FontThings themselves, I could very simply have bound like this:
<ComboBox
x:Name="OtherCombo"
ItemsSource="{x:Static hconv:ListItemPropertyGetter.FontSizeThings}"
DisplayMemberPath="FontSize"
FontSize="{Binding SelectedItem.FontSize, ElementName=OtherCombo, FallbackValue=20}"
/>
If that fits in with what you're doing, it's by far the nicest way.

Different TextBox formatting in edit mode

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 };
}
}

Multibinding not working on TextBox.Text

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.

Resources