1
votes

At the first glance the task looks similar to WPF TextBlock Negative Number In Red

In my case I have to display in an ItemsControl a collection of Points. Each Point has several properties of type NumericValue that is ultimately a wrapper around Nullable<double>.

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

I display all those properties of a Point as TextBoxes. The class NumericValue has the property IsNegative and I want the Foreground of the corresponding Textbox to be red if IsNegative=True.

However, I'd prefer not to define this rule in a style for each individual TextBox but to make a single style with DataTrigger bound to IsNegative.

The simplified XAML looks like the one below.

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

Please help me with the binding definition for the DataTrigger of that single style.

2
> Please help me with the binding definition for the DataTrigger of that single style. Binding="{Binding IsNegative}"H.B.
Does not seem to work. I tried this with the original textbox binding, and then tried also <TextBox Text="{Binding Path=Data.Proposal}" ... /> that seems more logical to me in regards to {Binding IsNegative}. In any case the IsNegative property was not even called.Ivan Gerken
The unconditional setters of the style work fine.Ivan Gerken
If the DataContext is not the point itself, which you seem to indicate here you need to modify the binding, if that Data property is the point the binding would be {Binding Data.IsNegative} (which here is equivalent to {Binding Path=Data.IsNegative}). If a binding does not do anything you should try to debug it.H.B.

2 Answers

0
votes

Use the converter given at WPF TextBlock Negative number in red like this

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

You could follow How to: Conditional formatting using XAML in WPF. I think it will be helpful for you.