Adding Value To Binding Value in Xaml

10 בSeptember 2013

תגיות: , , ,
תגובה אחת

Sometimes we like to add any value to an existing one using markup xaml.

It is not smart but maybe useful to be able ..

For example you have logic string data which is used like this :

<TextBlock Text=”{Binding Name}” Width=”auto”/>

Now what do you actually wants is to do this:

<TextBlock Text=”{Binding Name} + “user ID :” Width=”auto”/>

Of course it impossible , but you can use converter to apply the feature.

The Converter may look like :

public class IdDetailsConverter : IValueConverter

    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            string current = (string)value;

 

            return current + "user ID :";

 

        }

 

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            throw new NotImplementedException();

        }

    }

And so , at your xaml bind it together like this :

<Window x:Class="WpfApplication1.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:localconverters="clr-namespace:WpfApplication1"

        Title="MainWindow" Height="350" Width="525">

   

    <Window.Resources>

        <localconverters:IdDetailsConverter x:Key="IdDetailsConverter"/>

    </Window.Resources>

    <Grid>

        <TextBlock Text="{Binding Name, Converter={StaticResource IdDetailsConverter}}"  Width="auto"/>

    </Grid>

</Window>

 

 

הוסף תגובה
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

תגובה אחת

  1. Avi_Avni11 בSeptember 2013 ב 13:24

    you can use StringFormat fot thing like this

    Reply