2015-09-25 62 views
0

我有一个已知的错误WPF datagrid.rowstyle结合

System.Windows.Data错误:2:无法找到目标element.BindingExpression理事FrameworkElement的或FrameworkContentElement上:路径=百分比;的DataItem = NULL;目标元素是'GradientStop'(HashCode = 81530);目标属性“偏移”(类型“双”)

在这部分代码:

 <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> 

我试图设置DataContext的代理,但没有奏效:

 <DataGrid.Resources> 
      <app:BindingProxy x:Key="proxy" Data="{Binding}" /> 
     </DataGrid.Resources> 

     [...] 
     <GradientStop Color="#FF2D2D30" Offset="{Binding Data.Percent, Source={StaticResource proxyRow}}" /> 

System.Windows.Data错误:40:BindingExpression路径错误: '百分比' 属性上 '对象' '' 视图模型 '(的HashCode = 37637549)' 未找到。 BindingExpression:路径= Data.Percent; DataItem ='BindingProxy'(HashCode = 3342738);目标元素是'GradientStop'(HashCode = 64874797);目标属性“偏移”(类型“双”)

这是明显的数据现在包含在控制这是我的ViewModel的DataContext的。

有什么建议吗?谢谢 !

回答

0

正如其他帖子中的建议,我认为这不适用于我的问题,但使用转换器非常适合!下面是一些参考:

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; 
    } 

而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>