如何将数据模板分配给文本框wpf

如何将数据模板分配给文本框wpf

本文介绍了如何将数据模板分配给文本框wpf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TextBox 应该显示某些访问权限的屏蔽美元金额.我创建了一个转换器类(继承自 IValueConverter),通过实现 convert 方法来处理屏蔽.

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)

如果需要屏蔽,第三个参数为 true,否则为 false.

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

这样称呼:

CurrencyCOnverter converter = new CurrencyConverter();

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

我在 UI 上有大约 12 个文本框.我没有在 12 个地方这样做,而是在 Resource 字典中定义了 DataTemplates,如下所示:

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>

我的问题:有没有一种方法可以通过创建自定义文本框来将此模板分配给文本框,就像我们为 ListBox 分配数据模板一样?

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?

谢谢,

梅根.

推荐答案

您可以使用 ContentControl 来显示您的 DataTemplate.在这种情况下,我更喜欢的另一个想法是使用样式.下面的代码显示了两者兼而有之.

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>

这篇关于如何将数据模板分配给文本框wpf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 12:42