我正嘗試使用列來構建圖表。這些列將根據價值有不同的顏色。 我使用WPF和Databinding的MVVM。 如何我試圖做(進入我的視圖模型):使用MVVM和Databinding在WPF中動態更改圖表的顏色
private Int16 colorValue;
public Int16 ColorValue
{
get { return colorValue; }
set
{
colorValue = value;
if (ColorValue < 20)
ColorType = new SolidColorBrush { Color = Colors.Aqua };
if (ColorValue < 40)
ColorType = new SolidColorBrush { Color = Colors.Gray };
if (ColorValue >= 41)
ColorType = new SolidColorBrush { Color = Colors.Black };
}
}
private Brush colorType;
public Brush ColorType
{
get { return colorType; }
set
{
if (value != null)
{
colorType = value;
OnPropertyChanged("ColorType");
}
}
}
進入我的XAML(可這是靜態資源更改列顏色屬性):
<Style x:Key="ColorByGradeColumn" TargetType="DVC:ColumnDataPoint">
<Setter Property="Background" Value="DarkGray"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate
TargetType="DVC:ColumnDataPoint">
<Border
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid Background="{Binding ColorType}">
<Rectangle>
<Rectangle.Fill>
<LinearGradientBrush>
<GradientStop Color="#77ffffff" Offset="0"/>
<GradientStop Color="#00ffffff" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Border BorderBrush="#ccffffff" BorderThickness="1">
<Border BorderBrush="#77ffffff" BorderThickness="1"/>
</Border>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我的圖表爲XAML中:
<Grid Grid.Column="2" Height="368" HorizontalAlignment="Left" Name="grid1" VerticalAlignment="Bottom" Width="1009" Grid.ColumnSpan="4" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="198*" />
<ColumnDefinition Width="191*" />
</Grid.ColumnDefinitions>
<DVC:Chart x:Name="ColumnChart"
Grid.ColumnSpan="2">
<DVC:ColumnSeries
AnimationSequence="FirstToLast"
FlowDirection="LeftToRight"
Title="Largura"
ItemsSource="{Binding Path=Placas, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ToolTip="{Binding Path=Slab.InfThick}"
DependentValueBinding="{Binding Path=Slab.InfThick}"
IndependentValueBinding="{Binding Path=Slab.SlabId}"
DataPointStyle="{StaticResource ColorByGradeColumn}">
</DVC:ColumnSeries>
</DVC:Chart>
</Grid>
所以...我的圖表使用ColumnSeries通過定義到DataPointStyle中的靜態資源獲取它的屬性。 StaticResource'ColorByGradeColumn'我已經綁定了我的屬性ColorType。 下面是問題...爲什麼不工作?我跟着這個鏈接介紹的步驟:
而且我真的不知道我錯過了什麼。
在此先感謝。
嗯,我試圖刪除那部分,但它沒有任何區別。此外,作者在鏈接上,我已經遵循解釋說,... 你的意思是,不是嗎? <矩形。填> <一個LinearGradientBrush> <漸變停止顏色= 「#77ffffff」 偏移= 「0」/> <漸變停止顏色= 「#00FFFFFF」 偏移= 「1」/> 一個LinearGradientBrush> –
這是奇怪的事情:如果我手動將顏色放在每個字段上,它可以工作,但綁定不會。然而,如果我把數據綁定在另一個對象的作品。 所以,我的財產,可以使顏色更改工作,我的問題是圖表。 –