2
我想通過附加屬性使用來自Thomas Levesque的技巧爲DataGridTextColumn創建參數化樣式。但是,我無法爲我的案子工作。WPF:這個DataGridTextColumn風格有什麼問題?
基本上,我想這
<DataGridTextColumn Header="Today Chg $" Binding="{Binding TodaysValueChange, StringFormat=N2}" IsReadOnly="True">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell" BasedOn="{StaticResource RightAlignedCellStyle}">
<Setter Property="Foreground" Value="{Binding Path=TodaysValueChange, Converter={StaticResource PriceChangeToColor}}"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
轉換成該
<DataGridTextColumn Header="Today Chg $" Binding="{Binding TodaysValueChange, StringFormat=N2}" IsReadOnly="True" CellStyle="{StaticResource ColoredCell}" ul:ThemeProperties.SignValue="{Binding TodaysValueChange}" ElementStyle="{StaticResource CellRightAlign}"/>
不過,我得到這個錯誤: 「A不能一 'DataGridTextColumn' 集合中使用的 '綁定' 。綁定只能在DependencyObject的的一個DependencyProperty設置「爲結合TodaysValueChange到UL:ThemeProperties.SignValue。」我不知道它是什麼抱怨
這是我ThemeProperties:
public static class ThemeProperties
{
public static double GetSignValue(DependencyObject obj)
{
return (double)obj.GetValue(SignValueProperty);
}
public static void SetSignValue(DependencyObject obj, double value)
{
obj.SetValue(SignValueProperty, value);
}
public static readonly DependencyProperty SignValueProperty = DependencyProperty.RegisterAttached("SignValue", typeof(double), typeof(ThemeProperties), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.Inherits));
}
這是App.xaml中我的風格資源:
<Style x:Key="ColoredCell" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
<Setter Property="Foreground" Value="{Binding Path=ul:ThemeProperties.SignValue, Converter={StaticResource PriceChangeToColor}}"/>
</Style>