2012-05-15 24 views
1

的第一眼任務類似於 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的所有屬性顯示爲TextBoxes。 NumericValue類具有屬性IsNegative,如果IsNegative = True,我希望相應文本框的前景爲紅色。

但是,我不希望爲每個單獨的文本框的樣式定義此規則,而是要將DataTrigger綁定到IsNegative時創建一個樣式爲的單一樣式。

簡化的XAML看起來像下面那樣。

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

請幫我一下該單一樣式的DataTrigger的綁定定義。

+0

>請幫我看看該單一樣式的DataTrigger的綁定定義。綁定=「{綁定IsNegative}」 –

+0

似乎不起作用。我試着用原始文本框綁定,然後嘗試,這對於{Binding IsNegative}來說似乎更符合邏輯。無論如何,IsNegative屬性甚至沒有被調用。 –

+0

風格的無條件setters工作正常。 –

回答

0

使用在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> 
+0

感謝您的建議。我寧願保留XAML中的所有着色內容,所以我只會將這種方法作爲最後的手段。 –

相關問題