How to assign a datatemplate to textbox wpf

2019-02-27 10:23发布

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.

标签: wpf textbox
1条回答
劳资没心,怎么记你
2楼-- · 2019-02-27 10:47

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>
查看更多
登录 后发表回答