1

I have a TextBlock which is bound to an Integer property of my model. The TextBlock int property increases its count from 0 to 99. I first show TextBlocks 0-9 in a ListView.

If there are more than 10 TextBlocks, I want the first 10 TextBlocks numbered 0-9 to show as 00, 01 , 02.. 09. I can use the string.Format method in WPF to achieve this behavior. But if there are fewer than 10 TextBlocks they should be numbered as 0, 1, 2 -- 9.

How can I achieve this behaviour? Can I use MultiBinding Converter? If yes, help me with a sample.

Here is the code:

<ListView ItemsSource= "{Binding}"> <!-- Binding to a collection which has the Tag Id property --> <Grid x:Name="TagNum_grid" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="1,3,0,0" Grid.Column="1" > <TextBlock x:Name="DefaultIDtextblock" Margin="1,0" Text="{Binding Path=TagID}" TextWrapping="Wrap" Foreground="#FFA0A0A0" /> </Grid> </ListView> 

2 Answers 2

1

As you mentioned, you could use MultiBinding, where the first value is the tag id and the second is the number of elements.

First, define the value converter:

public class MyConverter : MarkupExtension, IMultiValueConverter { public override object ProvideValue(IServiceProvider serviceProvider) { return this; } public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { int tagId = (int)values[0]; int itemCount = (int)values[1]; if (itemCount >= 10 && tagId < 10) { return "0" + tagId; } return tagId; } } 

Then bind the values, using the above converter

<ListView ItemsSource="{Binding}"> <Grid x:Name="TagNum_grid" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="1,3,0,0" Grid.Column="1" > <TextBlock x:Name="DefaultIDtextblock" Margin="1,0" TextWrapping="Wrap" Foreground="#FFA0A0A0" > <TextBlock.Text> <MultiBinding Converter="{local:MyConverter}"> <Binding Path="TagID" /> <Binding Path="Items.Count" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ListView}" /> </MultiBinding> </TextBlock.Text> </TextBlock> </Grid> </ListView> 
Sign up to request clarification or add additional context in comments.

1 Comment

Works great... Thanks a lot Adi!!! Just a small change I did here for the check in the converter if (itemCount > 10 && tagId < 10).
0

You don't need a MultiValueConverter, a simple converter is enough.

What you need to do is bind the collection itself to the ConverterParameter property. Now when the converter is hit, you can check the count of the collection. If it is under 10 you can just let the value pass if not, just format the value and add the leading zeros as you wish.

3 Comments

I will give this a try and let you know! Thanks
ConverterParameter is not a dependency property and can hence not be bound.
stackoverflow.com/questions/4010772/binding-stringformat should get you pointed in the right direction.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.