I have a problem with a multibinding:
<Canvas>
<local:SPoint x:Name="sp" Width="10" Height="10">
<Canvas.Left><!-- irrelevant binding here --></Canvas.Left>
<Canvas.Top>
<MultiBinding Converter="{StaticResource myConverter}" Mode="TwoWay">
<Binding ElementName="cp1" Path="(Canvas.Top)"/>
<Binding ElementName="cp1" Path="Height"/>
<Binding ElementName="cp2" Path="(Canvas.Top)"/>
<Binding ElementName="cp2" Path="Height"/>
<Binding ElementName="sp" Path="Height"/>
<Binding ElementName="sp" Path="Slope" Mode="TwoWay"/>
</MultiBinding>
</Canvas.Top>
</local:SPoint>
<local:CPoint x:Name="cp1" Width="10" Height="10" Canvas.Left="20" Canvas.Top="150"/>
<local:CPoint x:Name="cp2" Width="10" Height="10" Canvas.Left="100" Canvas.Top="20"/>
</Canvas>
In order to calculate the Canvas.Top value, myConverter needs all the bound values. This works great in Convert(). Going the other way, myConverter should ideally calculate the Slope value (Binding.DoNothing for the rest), but it needs the other values in addition to the Canvas.Top one passed to ConvertBack(). What is the right way to solve this?
I have tried making the binding OneWay and create an additional multibinding for local:SPoint.Slope, but that causes infinite recursion and stack overflow. I was thinking the ConverterParameter could be used, but it seems like it's not possible to bind to it.
The approach may not be the most elegant one but it does work around the limits of multi bindings and the fact that ConverterParameter is not a DependencyProperty.
Make your converter derive from FrameworkContentElement. Define a number of DependencyProperty proprerties. You will need as many of them as there are values that you need on both ends of conversion.
Since your converter normally sits in XAML resources, DataContext and ElementName bindings won't work without some help from DataContextSpy and/or ElementSpy, depending on your particular binding needs.
The oddity of this approach is that you will have two identical bindings, one in <MultiBinding> section of XAML and the other one in Resources where you define your converter. The former one is needed to trigger the MultiBinding while the latter one actually provides the value in ConvertBack direction.
A downside is that resources are shared and thus if you need the same functional converter somewhere else on the page, you might need to declare a second one in your resources. This is only if two MultiBindings need different parameters. If parameters are shared, you may as well use the same resource. You might also try x:Shared="False" on the converter resource but I am not sure if that would work.
Related
So the issue is that when I normally bind a single text item to the Text in a TextBlock, the syntax is as follows:
<TextBlock ... Text="{Binding Attributes[StatusDateTime]}" /> //WORKS GREAT!
Now, background is that the DataContext for this part of the app is the output of a 3rd party API. "Attributes" is a collection of KeyValuePair, which is a property of the parent object. StatusDateTime is the key for the value I am returning. The syntax above works just great! So the object would look something like: theDataContextObject.Attributes[StatusDateTime].
BUT, if I need to combine multiple attributes into one TextBlock that's when things get hairy. I have no idea how to access the key from an actual Binding tag:
<TextBlock Grid.Row="0" Grid.ColumnSpan="3" Style="{StaticResource popupText}">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} at {1}">
<Binding Path="{Attributes[Type]}"/>
<!--<Binding Path="StatusDateTime" />-->
<Binding Path=Attributes[Type]/>
<Binding Path="Type" ElementName="Attributes"/> //the element has no
//name it's just the datacontext
<Binding Path="[StatusDateTime]" />
<Binding Path="Attributes[StatusDateTime]"/>
</MultiBinding>
</TextBlock.Text>
I know the number of examples above does not match the stringformat, I was just pasting examples of my guesses into the multibinding of how to get these values bound.
Apologies if this is a duplicate with another question. I am not sure what to even call this kind of binding where I'm just passing Attributes[StatusDateTime] directly to binding. Keep in mind that Attributes is not the name of an object, it's a property of the object passed to datacontext of the TextBlock's parent control.
So how do I bind to the value of key in the KeyValuePair collection when i have to use a Binding tag inside a MultiBinding?
I've written a general purpose prefix operator based multibinding converter that takes a list of binding values that can be either operators or values, it then computes the value based on the pre-defined behaviour of the defined set of operators.
So like this:
<MenuItem.Background>
<MultiBinding Converter="{StaticResource ArithmeticsConverter}">
<Binding Source="?"/>
<Binding Path="IsDirty"/>
<Binding>
<Binding.Source>
<SolidColorBrush Color="#dddf05"/>
</Binding.Source>
</Binding>
<Binding>
<Binding.Source>
<SolidColorBrush Color="#F0F0F0"/>
</Binding.Source>
</Binding>
</MultiBinding>
This returns #dddf05 if Dirty, otherwise #f0f0f0.
So this is very convenient since it's pretty much the only converter I need now, the xaml is verbose but at least I don't have to write converters for every specific case.
My problem is that I want to do something like:
?
=
null
SomeVariable
Visible
Collapsed
And set it to
<MyControl.Visible>
I.e. if SomeVariable is null, return Visible, otherwise Collapsed. But I havn't been able to reference Visibility values (or more generally, system enum values) like I am referencing color values above. I know it must be possible somehow since one can easily reference these values inline.
You can use these:
<Visibility>Visible</Visibility>
<Visibility>Hidden</Visibility>
<Visibility>Collapsed</Visibility>
The XAML processor will basically take the inner string of the tag and parse/convert it to enum value.
I have a TextBlock in my view that I want to always display the number 1, converted into the local currency. (e.g. $1 in US, £1 in UK, etc). I have a value converter that can do this, but I don't know how to apply a value converter to the value of 1 without getting my data from a databinding.
I can think of two solutions, but they each have their problems, and I'm looking for something more elegant:
Create a property on my ViewModel that just holds and returns the value 1 and bind to it. Then add my converter to this binding. This seems backwards, particularly as this is view-only code.
Make a binding point to an existing property and modify my converter to ignore the value given to it, and instead use the parameter to give it the number 1. This feels unintuitive to other programmers, as they'll be confused as to why I'm binding to a different property there.
Is there some way of applying a converter without first creating a binding?
If you want for this to be relatively readable from XAML alone, you can always do it like this:
<Label>
<Label.Resources>
<system:Int32 x:Key="defaultValue">1</system:Int32>
</Label.Resources>
<Label.Content>
<Binding Source="{StaticResource defaultValue}"
Converter="{StaticResource CurrencyConverter}" />
</Label.Content>
</Label>
I had similar situation, I needed to convert static enum value placed directly in xaml to be converted by my custom converter without using databinding.
<Border>
<Border.Background>
<Binding Source="{x:Static enumeration:ColorType.Main}"
Converter="{StaticResource ColorConverter}" />
</Border.Background>
</Border>
There is a concept of Multibinding in WPF, which is really useful if we want to bind some UI Control that depends on multiple values, like in this one.
I am trying to do the same with ProgressBar, like I am using ProgressBar to display how much storage is used by the users and it depends on two properties.
UsedStorage
TotalStorage
After searching, i could not find a way to bind Value property of ProgressBar with multiple properties and custom convertor.
Something like (Just a concept)
<ProgressBar Width="172" Height="16" >
<ProgressBar.Value>
<MultiBinding Converter="{StaticResource myConverter}">
<Binding Path="UsedStorage" RelativeSource="{RelativeSource AncestorType={x:Type Window}}"/>
<Binding Path="TotalStorage" RelativeSource="{RelativeSource AncestorType={x:Type Window}}"/>
</MultiBinding>
</ProgressBar.Value>
</ProgressBar>
But problem is there is nothing like MultiBinding under ProgressBar.Value. So the question is,
Is there a way to MultiBind ProgressBar Value?
Even though the Visual Studio intellisense might not show that, it is perfectly valid.
However, Using RelativeSource TemplatedParent only works inside a ControlTemplate. It is unclear to me where are you trying to get your values from.
I'm making a WPF application and I need a value convertor with more than 1 binding path, is that possible?
It works with 1 path so far:
Binding="{Binding Path=Price, Converter={StaticResource vPriceConvertor}}"
I wanna give the Price and the discount to the converter so he can calculate the endprice.
Value converters are intended... for value conversion. That's why they're value converters.
To do what you want, you should make a property EndPrice in view model, and calculate its value in view model. Why are you trying to bring a non-UI logic into UI??
Look at the MultiBinding class. For example:
<TextBlock Name="textBlock" DataContext="{StaticResource myViewModel}">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource vPriceConvertor}"
ConverterParameter="myParameter">
<Binding Path="Price"/>
<Binding Path="Discount"/>
<!-- Insert other paths here -->
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Implement IMultiValueConverter instead of IValueConverter to actually do the conversion, since it supports multiple sources.
Parys -
You could always create a collection (in View Model) which contains Price and Discount and then pass it through your XAML (through IValueConverter).
But to re-iterate #Dennis and everybody else point - then your converter will have calculation logic - which is not recommended.