tags:

views:

29

answers:

1

Hi,

TextBox is supposed to show masked dollar amount for certain access privileges. I created a converter class(inheriting from IValueConverter) to handle masking by implementing the convert method.

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

The third parameter is passed true if masking is needed else false.

Called like this:

CurrencyCOnverter converter = new CurrencyConverter();

this._textbox1.Text = converter.Convert(Amount, typeof(string), !this.IsSuperUser,
                          CurrentCulture).ToString();

I have about 12 textboxes on UI. Instead of doing this at 12 places, I defined DataTemplates in Resource dictionary which looks like this:

<DataTemplate x:Key="MaskNormalBackgroundTbx">

 <TextBlock TextAlignment="Right" VerticalAlignment="Center"
            TextWrapping="WrapWithOverflow" 
            Text="{Binding "Amount" 
                   Converter={StaticResource CurrencyDisplayConverter}, 
                   ConverterParameter=true}" />    
</DataTemplate>

 <DataTemplate x:Key="NoMaskNormalBackgroundTbx">

 <TextBlock TextAlignment="Right" VerticalAlignment="Center" 
            TextWrapping="WrapWithOverflow" 
            Text="{Binding "Amount" 
                   Converter={StaticResource CurrencyDisplayConverter}, 
                   ConverterParameter=false}" />    
 </DataTemplate>

My Question: Is there a way I can assign this template to textbox by creating a custom textbox just like we assign data templates for ListBox?

Thanks,

Megan.

A: 

You can use a ContentControl to display your DataTemplate. Another idea, which I prefer in this case, is to use styles. Below code shows hot to do both.

<Window x:Class="Test.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Test="clr-namespace:Test"
    Height="300" Width="300">

    <Window.Resources>

        <Test:CurrencyDisplayConverter x:Key="CurrencyDisplayConverter" />

        <DataTemplate x:Key="MaskNormalBackgroundTbxDT">
            <TextBlock TextAlignment="Right" VerticalAlignment="Center" 
            TextWrapping="WrapWithOverflow"  
            Text="{Binding Converter={StaticResource CurrencyDisplayConverter}, ConverterParameter=true}" />
        </DataTemplate>

        <Style x:Key="MaskNormalBackgroundTbxStyle" TargetType="TextBlock">
            <Setter Property="TextAlignment" Value="Right" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="TextWrapping" Value="WrapWithOverflow" />
            <Setter Property="Text" Value="{Binding Path=Amount, Converter={StaticResource CurrencyDisplayConverter}, ConverterParameter=true}" />
        </Style>

    </Window.Resources>
    <StackPanel>

        <ContentControl
            Content="{Binding Path=Amount}" 
            ContentTemplate="{StaticResource MaskNormalBackgroundTbxDT}" />

        <TextBlock 
            Style="{StaticResource MaskNormalBackgroundTbxStyle}" />

    </StackPanel>

</Window>
Wallstreet Programmer
Wallstreet Programmer,Thanks for your response. I already have a custom textbox with label and other customized control templates and properties in place. I would add onto that rather than replacing the textbox control itself. Please let me know if you have any ideas on how to make it work with a textbox control.Thanks.
Use a style then like in above sample (MaskNormalBackgroundTbxStyle)
Wallstreet Programmer
Inserted this trigger inside the style of custom textbox: <Trigger Property="Tag" Value="True"> <Setter Property="Text" Value="{Binding Converter={StaticResource CurrencyDisplayConverter}, ConverterParameter=true}"/> </Trigger> <Trigger Property="Tag" Value="False"> <Setter Property="Text" Value="{Binding Converter={StaticResource CurrencyDisplayConverter}, ConverterParameter=false}"/> </Trigger> Not sure why, text in textbox is never masked.