I have 2 string variables var1,var 2 .I need to show
For information on returns and exchanges please visit {var 1} or call {var2}.
any xaml solution available for that?.
You can use a MultiBinding, like this:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="For information on returns and exchanges please visit {0} or call {1}.">
<Binding Path="SomeProperty"/>
<Binding Path="SomeOtherProperty"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Note that the SomeProperty and SomeOtherProperty are simple bindings to the DataContext of the TextBlock, such as the View Model or underlying Model.
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 have this code :
<Label>
<Label.Content>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{} created on {0} by">
<Binding Path="CreationDate" StringFormat="{}{0:dd/MM/yyyy}" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</LabeledLabel.Content>
</Label>
OUTPUT
I always get this created on 21/09/2014 00:00:00 by
I tried StringFormat="d", but it didn't work too.
What's the problem with my code ?
You've only got one Binding Path, so you'll only ever get the date and time. Basically, you need to add a Binding element for your person data type. It should be more like this:
<Label>
<Label.Content>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{} created on {0:dd/MM/yyyy} by {1}">
<Binding Path="CreationDate" />
<Binding Path="SomeEmployeeObject.Name" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</LabeledLabel.Content>
</Label>
Note that you can also set the DateTime StringFormat using the MultiBinding.StringFormat property, instead of adding another on the first Binding object. You also needed to add the {1} to the end of the MultiBinding.StringFormat so that it would output the second (person related) value.
Please see the MultiBinding Class page on MSDN for further information.
UPDATE >>>
I don't understand why putting the StringFormat property on the MultiBinding element has a different behaviour compared to the first element
It doesn't... I could have left it there, but I moved it because you were already using a StringFormat. Using StringFormat property on a MultiBinding is virtually the same as using the string.Format method. Using the method, this is equivalent to what you had in your XAML:
string.Format("created on {0:dd/MM/yyyy} by ", someDate);
And this is equivalent to what I put in your XAML:
string.Format("created on {0:dd/MM/yyyy} by {1}", someDate, someEmployee.Name);
Hopefully, you can now see the difference.
I am trying to multibind a formatted double value to a text box. I have a converter which takes in a double and a Formatter object and returns a formatted string to be displayed. The double is bound to a particular data source and the formatter is a property in the view model. The problem I'm having is that I'm unable to bind to the view model property. This is my code in xaml
<StackPanel Grid.Row="0" Grid.Column="1">
<TextBlock HorizontalAlignment="Left" Style="{StaticResource HintDataItemsStyle}">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource FormatConverter}">
<Binding Path="OpenValue" />
<Binding Path="XLabelFormatterY1" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
This is the property in the view model
private ILabelFormatter _labelFormatterY1;
public ILabelFormatter XLabelFormatterY1
{
get { return _labelFormatterY1; }
set
{
_labelFormatterY1 = value;
OnPropertyChanged("XLabelFormatterY1");
}
}
So, in my converter I'm able to pick up the value for "OpenValue" ,but the runtime is unable to find XLabelFormatterY1. Most of the examples I have seen for multibinding bind to gui components. I'm trying to bind to the view model and would appreciate all help.
Old question but without answer. I beleive that you are looking for this solution. If this answer doesn't work for you, try to explicitly set NotifyOnSourceUpdated="True" in the binding. And also double check if you have set correct AncestorType as wookietomwookie says in his answer.
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.
Is it possible in WPF to bind to 2 elements?
For example I'd like to display something like myserver.com:80 in a textbox.
So to do this I'd like to bind to both a Host field then add a ":" then bind to a port field in my object all for the same label content.
In WPF 4/3.5SP1 you can use a MultiBinding in conjunction with StringFormat:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{0}:{1}">
<Binding Path="Host"/>
<Binding Path="Port"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Prior to WPF 4 you can still use a MultiBinding but would need to write your own converter instead.
An alternative to both these approaches is do MVVM and expose a property that does the concatenation for the view, then the view just binds directly to that property.