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

编程入门 行业动态 更新时间:2024-10-24 20:17:57
本文介绍了如何将数据模板分配给文本框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="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/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

本文发布于:2023-11-10 00:42:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573894.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   文本框   模板   数据   wpf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!