2015-09-25 94 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>