Reputation: 3545
I've got a known error
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element.BindingExpression:Path=Percent; DataItem=null; target element is 'GradientStop' (HashCode=81530); target property is 'Offset' (type 'Double')
on this part of code :
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding State}" Value="Started">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="1,0" StartPoint="0,0">
<GradientStop Color="Green" Offset="0" />
<GradientStop Color="#FF2D2D30" Offset="{Binding Percent}" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</DataTrigger>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
I've tried to set the DataContext
to a proxy but it did not work :
<DataGrid.Resources>
<app:BindingProxy x:Key="proxy" Data="{Binding}" />
</DataGrid.Resources>
[...]
<GradientStop Color="#FF2D2D30" Offset="{Binding Data.Percent, Source={StaticResource proxyRow}}" />
System.Windows.Data Error: 40 : BindingExpression path error: 'Percent' property not found on 'object' ''ViewModel' (HashCode=37637549)'. BindingExpression:Path=Data.Percent; DataItem='BindingProxy' (HashCode=3342738); target element is 'GradientStop' (HashCode=64874797); target property is 'Offset' (type 'Double')
Which is obvious as the Data now contains the DataContext of the Control which is my ViewModel.
Any suggestion ? Thanks !
Upvotes: 1
Views: 1488
Reputation: 3545
As suggested in other posts, I thought it would not apply to my issue, but using a converter suits very well ! Here are some references:
Binding GradientStop works but reports error
How to bind GradientStop Colours or GradientStops Property in Silverlight?
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
Model.ListParameters parameters = (Model.ListParameters)value;
if(parameters !=null)
{
var start = new GradientStop();
start.Color = Colors.Green;
start.Offset = 0;
var stop = new GradientStop();
stop.Color = (Color)ColorConverter.ConvertFromString("#FF2D2D30");
stop.Offset = parameters .Percent;
var result = new LinearGradientBrush();
result.StartPoint = new Point(0, 0);
result.EndPoint = new Point(1, 0);
result.GradientStops.Add(start);
result.GradientStops.Add(stop);
return result;
}
return null;
}
And the XAML
<UserControl.Resources>
<app:GradientProgressConverter x:Key="GradientProgressConverter" />
</UserControl.Resources>
<DataGrid>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding State}" Value="Started">
<Setter Property="Background" Value="{Binding Converter={StaticResource GradientProgressConverter}}" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</<DataGrid>
Upvotes: 0