I have a multilanguage application which contains two dictionary en.xaml and it.xaml, suppose that I have the following situation:
I need to display a default text in a TextBlock such as: Contact saved 0 in memory using a DynamicResource key that contains the text above.
Unfortunately xaml doesn't allow to use DynamicResource in a StringFormat or as FallbackValue so I used this code:
<TextBlock Tag="{DynamicResource defaultContactStr}">
<TextBlock.Text>
<PriorityBinding>
<Binding Path="ContactSaved" />
<Binding Path="Tag" RelativeSource="{RelativeSource Mode=Self}" />
</PriorityBinding>
</TextBlock.Text>
this will display only 0 which is the default value of the proeprty ContactSaved, but I need to display: Contact saved 0 in memory, or if the value change: Contact saved 5 in memory etc...
how can I manage this situation?
Using a StringFormat works only if the format (the translation in your case) is known at compile-time:
<TextBlock>
<TextBlock.Text>
<Binding Path="ContactSaved" StringFormat="{}Contact saved {0} in memory" />
</TextBlock.Text>
</TextBlock>
Othwerwise you may handle this using a converter and a MultiBinding:
public class CustomConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
string resource = values[0] as string;
string contactSaved = values[1].ToString();
return string.Format(resource, contactSaved);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
XAML:
<TextBlock Tag="{DynamicResource defaultContactStr}">
<TextBlock.Text>
<MultiBinding>
<MultiBinding.Converter>
<local:CustomConverter />
</MultiBinding.Converter>
<Binding Path="Tag" RelativeSource="{RelativeSource Mode=Self}" />
<Binding Path="ContactSaved" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
en.xaml:
<system:String x:Key="defaultContactStr">Contact saved {0} in memory</system:String>
Remember that XAML is nothing but a markup language.
Related
I have been playing with wpf lately and got stuck at a point where I would like to use two, binding path values sum as the binding of a third control. I tried using value converter but to no success. Would appreciate if any one can guide me to the right path.
Here is what I have so far:
I have two encoders like
<my3:NVAngle Canvas.Left="400" Canvas.Top="610" BindableValueExtended="120" BindableValueRetracted="0" AnimationDuration="2" x:Name="nvAArm3Rotate" BindableValue="0" InvertExtendedTag="False" InvertRetractedTag="False" />
<my3:NVAngle Canvas.Left="400" Canvas.Top="610" BindableValueExtended="50" BindableValueRetracted="0" AnimationDuration="2" x:Name="nvAArm2Rotate" BindableValue="0" InvertExtendedTag="False" InvertRetractedTag="False" />
and I would like to rotate an image with an angle which is the sum of above two angle encoders. Something like:
<Image Canvas.Left="100" Canvas.Top="200" Source="/Images/ExtraInter.png" Stretch="Fill" Width="117" Height="15" RenderTransformOrigin="0.95, 0.5">
<Image.RenderTransform>
<RotateTransform>
<RotateTransform.Angle>
<MultiBinding Converter="{StaticResource BundleArm3Full}">
<Binding ElementName="nvABundleArm2Rotate" Path="BindableValue" />
<Binding ElementName="nvABundleArm3Rotate" Path="BindableValue" />
</MultiBinding>
</RotateTransform.Angle>
</RotateTransform>
<!--120-->
</Image.RenderTransform>
</Image>
and my converter is:
public class MultipleBindingAddConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var doubleValues = values.Cast<double>().ToArray();
var resultSum = doubleValues.Sum().ToString();
return resultSum;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
I still see no rotation of the Image although I can guarantee that the BindableValues are changing. Would appreciate any help.
Thanks!
You could use Multibinding with a Converter that does the sum.
<MultiBinding Converter="{StaticResource sumConverter}" >
<Binding Path="FirstValue"/>
<Binding Path="SecondValue"/>
</MultiBinding>
Most converters take no parameters, or one fixed parameter, and are easy to bind to:
<local:MyConverter x:Key="MyConverterInstance" />
<TextBox Text="{Binding Path=MyTime,
Converter={StaticResource MyConverterInstance},
ConverterParameter='yyyy/MM/dd'}" />
But if I want that format to be a dynamic property that the user can change, I can't do something like this, right?:
<TextBox Text="{Binding Path=MyTime,
Converter={StaticResource MyConverterInstance},
ConverterParameter={Binding Path=UserFormat}}" />
So my only option is to define a DependencyProperty on MyConverter for binding. But my converter definition is a StaticResource. I can't go
<local:MyConverter x:Key="MyConverterInstance"
Format="{Binding Path=UserFormat}"/>
because there's no DataContext on StaticResources. How can I set this up?
You cannot bind to a converterparameter but you can use Multibinding instead.
For example: http://www.switchonthecode.com/tutorials/wpf-tutorial-using-multibindings
or How to simply bind this to ConverterParameter?
(Alain) So just to translate that linked answer into something that matches this question:
<TextBlock>
<TextBlock.Resources>
<local:MyConverter x:Key="MyConverterInstance" />
</TextBlock.Resources>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource converter}">
<Binding Path="MyTime" />
<Binding Path="UserFormat" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
public class MyConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
DateTime time = (DateTime)values[0];
string format = values[1].ToString();
return time.ToString(format);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
I have two separate converters for visibility, one based on whether a field has been updated and one based on whether a field is allowed to be seen. I use the updatedField one for each text item on my page so that a star shows up next to an updated field. But some text items only are visible to some users based on permission levels.
For example:
<Image Visibility="{Binding ElementName=MyObject, Path=UpdatedFields, Mode=OneWay, Converter={StaticResource updatedFieldConverter}, ConverterParameter=FieldToTest}" Source="Properties:Resources.star_yellow" />
and
<TextBlock FontSize="21" Foreground="{DynamicResource LabelBrush}" Text="{x:Static Properties:Resources.Some_Text}" Visibility="{Binding Source={StaticResource allowedFields}, Path=Some_Text_Field, Converter={StaticResource visibilityConverter}}" />
My problem is that for the case of the permission-required fields I need to run both converters to determine if the star shows up. Is there a way to do a boolean "And" on the results of two converters?
I looked at this post but it doesn't seem to allow for different sets of parameters to be passed into to the two different converters.
-------Update--------
I also tried to create a MultiValueConverter with this xaml
<Image Grid.Row="4" Grid.Column="0" Source="star_yellow.png">
<Image.Visibility>
<MultiBinding Converter="{StaticResource combinedVisibilityConverter}" ConverterParameter="FieldToTest" >
<Binding ElementName="allowedFieldsModel" Path="Some_Text_Field" Mode="OneWay" />
<Binding ElementName="MyObject" Path="UpdatedFields" Mode="OneWay" />
</MultiBinding>
</Image.Visibility>
</Image>
But when it enters the converter both values are "DependencyProperty.UnsetValue". So I'm apparently doing something wrong here.
--------Solution---------
I had to modify to this, but then it worked.
<Image.Visibility>
<MultiBinding Converter="{StaticResource combinedVisibilityConverter}" ConverterParameter="FieldToTest">
<Binding Source="{StaticResource allowedFieldsModel}" Path="Some_Text_Field" />
<Binding Path="MyObject.UpdatedFields" />
</MultiBinding>
</Image.Visibility>
You could use a MultiBinding together with a short, hand made IMultiValueConverter.
Example:
<StackPanel>
<StackPanel.Resources>
<local:MultiBooleanToVisibilityConverter x:Key="Converter" />
</StackPanel.Resources>
<CheckBox x:Name="Box1" />
<CheckBox x:Name="Box2" />
<TextBlock Text="Hidden Text">
<TextBlock.Visibility>
<MultiBinding Converter="{StaticResource Converter}">
<Binding ElementName="Box1"
Path="IsChecked" />
<Binding ElementName="Box2"
Path="IsChecked" />
</MultiBinding>
</TextBlock.Visibility>
</TextBlock>
</StackPanel>
... and the converter ...
class MultiBooleanToVisibilityConverter : IMultiValueConverter
{
public object Convert(object[] values,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
bool visible = true;
foreach (object value in values)
if (value is bool)
visible = visible && (bool)value;
if (visible)
return System.Windows.Visibility.Visible;
else
return System.Windows.Visibility.Hidden;
}
public object[] ConvertBack(object value,
Type[] targetTypes,
object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Late to the party here but an easier solution is to just wrap the control in another control. I prefer this to having lots of Converters that do different things.
<Border Visibility="{Binding Value1, Converter={convertersDF:Converter_ValueToVisibility}}">
<ComboBox Visibility="{Binding Value2, Converter={convertersDF:Converter_ValueToVisibility}}"/>
</Border>
One thing that came to mind is, perhaps, instead of two different boolean fields, a single bit field created by ORing together updatedField and allowedField. Then you can have three value converters, all operating on the same field.
Or just calculate another field in your data model that does the ANDing there. That's probably more efficient (in terms of runtime).
You could pass an array of two objects to the converter in the ConverterParameter - constructing the array in XAML.
I ask because it doesn't seem to work.
Assume we're binding to the following object:
public class HurrDurr
{
public string Hurr {get{return null;}}
public string Durr {get{return null;}}
}
Well, it would appear that if we used a MultiBinding against this the fallback value would be shown, right?
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} to the {1}"
FallbackValue="Not set! It works as expected!)">
<Binding Path="Hurr"/>
<Binding Path="Durr"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
However the result is, in fact, " to the ".
Even forcing the bindings to return DependencyProperty.UnsetValue doesn't work:
<TextBlock xmnlns:base="clr-namespace:System.Windows;assembly=WindowsBase">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} to the {1}"
FallbackValue="Not set! It works as expected!)">
<Binding Path="Hurr"
FallbackValue="{x:Static base:DependencyProperty.UnsetValue}" />
<Binding Path="Durr"
FallbackValue="{x:Static base:DependencyProperty.UnsetValue}" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Tried the same with TargetNullValue, which was also a bust all the way around.
So it appears that MultiBinding will never ever use FallbackValue. Is this true, or am I missing something?
A little more messing around and I found that a converter can return the UnsetValue I need:
class MultiValueFailConverter : IMultiValueConverter
{
public object Convert(
object[] values,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
if (values == null ||
values.Length != 2 ||
values.Any(x=>x == null))
return System.Windows.DependencyProperty.UnsetValue;
return values;
}
public object[] ConvertBack(
object value,
Type[] targetTypes,
object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException("Too complex hurt brain.");
}
}
However, this seems like a dirty filthy hack. I'd think a scenario like this would be accounted for in the framework. I can't find anything in Reflector, however.
This is a bit of an old question, but it could use some explanation.
From the FallbackValue documentation:
A binding returns a value successfully if:
The path to the binding source resolves successfully.
The value converter, if any, is able to convert the resulting value.
The resulting value is valid for the binding target (target) property.
If 1 and 2 return DependencyProperty.UnsetValue, the target property
is set to the value of the FallbackValue, if one is available. If
there is no FallbackValue, the default value of the target property is
used.
In the example provided, the binding successfully resolves to the Hurr and Durr properties. Null is valid value for a string which means the binding is valid.
In other words, the FallbackValue is used when the binding is unable to return a value and in the example provided, the binding does provide a valid value.
Take for example each of the following snippets that are based off the original example:
Example 1
The Hurr and Durr properties are bound correctly; null is a valid value and the FallbackValue will never be seen.
<TextBlock>
<TextBlock.Text>
<MultiBinding FallbackValue="Binding is valid. I will never be seen." StringFormat="{}{0} to the {1}">
<Binding Path="Hurr" />
<Binding Path="Durr" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Example 2
The Hurr and Durr properties are not bound correctly; the FallbackValue will be seen.
<TextBlock>
<TextBlock.Text>
<MultiBinding FallbackValue="Binding paths are invalid. Look at me." StringFormat="{}{0} to the {1}">
<Binding Path="xHurr" />
<Binding Path="xDurr" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Example 3
If one binding path is invalid, then the FallbackValue will be seen.
<TextBlock>
<TextBlock.Text>
<MultiBinding FallbackValue="One binding path is invalid. Look at me." StringFormat="{}{0} to the {1}">
<Binding Path="xHurr" />
<Binding Path="Durr" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Example 4
As with previous examples, the binding is correct, so the FallbackValue will not be used. Further, the FallbackValue for each of the child Binding properties of the MultiBinding parent should refer to a FallbackValue to be used for the target property of the MultiBinding, not for the child Bindings.
<TextBlock xmlns:base="clr-namespace:System.Windows;assembly=WindowsBase">
<TextBlock.Text>
<MultiBinding FallbackValue="Binding is valid. I will never be seen." StringFormat="{}{0} to the {1}">
<Binding FallbackValue="{x:Static base:DependencyProperty.UnsetValue}" Path="Hurr" />
<Binding FallbackValue="{x:Static base:DependencyProperty.UnsetValue}" Path="Durr" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Example 5
The binding is still valid even though a path is not provided in Binding properties since the binding will use whatever object it is bound to.
<TextBlock xmlns:base="clr-namespace:System.Windows;assembly=WindowsBase">
<TextBlock.Text>
<MultiBinding FallbackValue="Binding is still valid. I will never be seen." StringFormat="{}{0} to the {1}">
<Binding FallbackValue="{x:Static base:DependencyProperty.UnsetValue}" />
<Binding FallbackValue="{x:Static base:DependencyProperty.UnsetValue}" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Example 6
Finally, if a converter is added to any of the Binding properties to force an UnsetValue, then the MultiBinding FallbackValue will be seen:
Converter
internal class ForceUnsetValueConverter : IValueConverter
{
#region Implementation of IValueConverter
public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
{
return DependencyProperty.UnsetValue;
}
public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
{
throw new NotImplementedException();
}
#endregion
}
XAML
<TextBlock>
<TextBlock.Text>
<MultiBinding FallbackValue="Binding is valid, but look at me. I'm an UnsetValue." StringFormat="{}{0} to the {1}">
<Binding Converter="{StaticResource ForceUnset}" Path="Hurr" />
<Binding Path="Durr" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
I currently have two text boxes which accept any number. I have a text block that takes the two numbers entered and calculates the average.
I was wondering if there was a way I could bind this text block to both text boxes and utilize a custom converter to calculate the average? I currently am catching the text changed events on both text boxes and calculating the average that way, but I am under the assumption data binding would be more efficient and easier.
You're looking for MultiBinding.
Your XAML will look something like this:
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource myConverter}">
<Binding Path="myFirst.Value" />
<Binding Path="mySecond.Value" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
With reasonable replacements for myConverter, myFirst.Value, and mySecond.Value.
Create a converter that implements IMultiValueConverter. It might look something like this:
class AverageConverter : IMultiValueConverter
{
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int total = 0;
int number = 0;
foreach (object o in values)
{
int i;
bool parsed = int.TryParse(o.ToString(), out i);
if (parsed)
{
total += i;
number++;
}
}
if (number == 0) return 0;
return (total/number).ToString();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
A multivalue converter receives an object array, one for each of the bindings. You can process these however you need, depending on whether you're intending it for double or int or whatever.
If the two textboxes are databound, you can use the same bindings in the multibinding for your textblock (remembering to notify when the property changes so that your average is updated), or you can get the text value by referring to the textboxes by ElementName.
<TextBox Text="{Binding Value1}" x:Name="TextBox1" />
<TextBox Text="{Binding Value2}" x:Name="TextBox2" />
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource AverageConverter}">
<Binding ElementName="TextBox1" Path="Text" />
<Binding ElementName="TextBox2" Path="Text" />
<!-- OR -->
<!-- <Binding Path="Value1" /> -->
<!-- <Binding Path="Value2" /> -->
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Or, you could make a property in code behind, and bind the TextBlock to that ... I do that all the time, and it's a little simpler than making a converter, then doing that same code there.
Example: (in your code behind of the xaml):
public double AvgValue
{
get { return (valueA + valueB) / 2.0; }
}
And then, in your XAML:
<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=AvgValue}" />
That's a LOT simpler than a custom converter.
Just to add step-by-step procedure to Timothy's answer:
Setup the View.TextBlock.Text property to bind to the ViewModel.AvgValue property.
Catch the TextChanged event of the TextBox control, then set the AvgValue in the handler of that TextChanged event.
As part of that handler in step 2, make sure to raise a property change so that the TextBlock is updated.