c# - xaml 条件字符串格式

标签 c# xaml data-binding binding

我想用“条件”价格格式制作一个绑定(bind)

更准确地说,如果另一个属性为 true:显示不带百分比的价格,如果为 false,则显示带百分比的价格。有没有一种方法可以使用 xaml 处理这种情况,或者我应该只在我的代码隐藏代码中创建一个字符串价格属性?

最佳答案

您可以像这样使用DataTrigger WPF DataBinding with an conditional expression .对于其他格式的值,您可以使用不同的转换器

例如:

<UserControl.Resources>
    <converters:ToPercentage x:Key="ToPercentage"/>
</UserControl.Resources>

<TextBox>
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=AnotherProperty}" Value="True">
                    <Setter Property="Text" Value="{Binding Path=Price}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=AnotherProperty}" Value="False">
                    <Setter Property="Text" Value="{Binding Path=Price, Converter={StaticResource ToPercantage}}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

关于c# - xaml 条件字符串格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40978537/

相关文章:

wpf - 绑定(bind)到时间相关的属性

wpf - 尝试创建允许文本选择的 WPF 标签模板时出现格式问题

c# 修改 getter/setter 但保留简短形式

c# - .net/native 编码的 out string 和 StringBuilder 之间有什么区别?

c# - XML 序列化具有抽象基类的可序列化对象的通用列表

c# - 使用 ObservableCollection 进行动态数据显示

c# - Lumisoft IMAP 附件已保存 0 字节

c# - 如何正确创建带有图像的按钮

.net - WinRT/UWP 帧和页面缓存 : How to create new page instance on Navigate() and keep the page instance on GoBack()

html - 在 Polymer 中使用重复模板时,禁用数据绑定(bind)中的转义 HTML