【问题标题】:Single TextBox style to show negative numbers in red for all of them单个文本框样式以红色显示所有负数
【发布时间】:2012-05-22 20:33:28
【问题描述】:

乍一看,任务看起来类似于 WPF TextBlock Negative Number In Red

在我的情况下,我必须在 ItemsControl 中显示积分集合。每个 Point 都有几个 NumericValue 类型的属性,它们最终是 Nullable<double> 的包装器。

public class Point
{
    NumericValue Proposal { get; set; }
    NumericValue Accepted { get; set; }
    NumericValue Approved { get; set; }
    ... etc.
}

我将 Point 的所有这些属性显示为文本框。 NumericValue 类具有 IsNegative 属性,如果 IsNegative=True,我希望相应文本框的前景为红色。

但是,我不希望在每个单独的 TextBox 的样式中定义此规则,而是制作一个 single 样式,并将 DataTrigger 绑定到 IsNegative。

简化的 XAML 如下所示。

<ItemsControl ItemsSource="{Binding Path=Points}">
...
    <TextBox Text="{Binding Path=Data.Proposal.Value}" ... />
    <TextBox Text="{Binding Path=Data.Accepted.Value}" ... />
...
</ItemsControl>

请帮我解决该单一样式的 DataTrigger 的绑定定义。

【问题讨论】:

  • > 请帮我解决该单一样式的 DataTrigger 的绑定定义。 Binding="{Binding IsNegative}"
  • 似乎不起作用。我用原始的文本框绑定尝试了这个,然后也尝试了 这对我来说似乎更符合 {Binding IsNegative} 的逻辑。在任何情况下,甚至都没有调用 IsNegative 属性。
  • 风格的无条件设置器工作正常。
  • 如果 DataContext 不是点本身,您似乎在此处指出您需要修改绑定,如果 Data 属性是绑定将是 {Binding Data.IsNegative} 的点(这里是相当于{Binding Path=Data.IsNegative})。如果绑定没有做任何事情,您应该尝试debug it

标签: c# wpf xaml binding datatrigger


【解决方案1】:

像这样使用WPF TextBlock Negative number in red 给出的转换器

 <ItemsControl ItemsSource="{Binding Path=Points}">
        ...
        <TextBox Text="{Binding Path=Data.Proposal.Value}" Foreground="{Binding Data.Proposal.IsNegative, Converter={StaticResource valueToBackground}}" />
        ...
    </ItemsControl>

【讨论】:

  • 感谢您的建议。我更愿意将所有的着色内容保留在 XAML 中,所以我只会将此方法用作最后的手段。
【解决方案2】:

您可以关注How to: Conditional formatting using XAML in WPF。我想这对你会有帮助。

【讨论】:

    猜你喜欢
    • 2012-07-13
    • 2014-05-26
    • 2017-07-05
    • 2016-05-16
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多