2014-05-13 103 views
1

我下面的示例正常工作,它在Id.Updated屬性爲true時突出顯示Id列中的單元格。WPF DataGrid綁定到DataGridCell樣式的當前對象屬性

我想知道如何修改綁定表達式Binding="{Binding Id.Updated}"以便綁定到當前IssueElement對象的Updated屬性的正確列(不僅是Id一個)。

我想能夠做到這一點只有一種風格的所有列,而不是每列一種風格。

以下示例是DataGrid如何在我的應用程序中工作的簡化版本。

數據網格:

<DataGrid ItemsSource="{Binding IssueList}" AutoGenerateColumns="False" > 
    <DataGrid.Resources> 
     <Style x:Key="TestStyle" TargetType="{x:Type DataGridCell}"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding Id.Updated}" Value="True"> 
        <Setter Property="Background" Value="Green" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </DataGrid.Resources> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="Id" Binding="{Binding Id.Value}" CellStyle="{StaticResource TestStyle}" /> 
     <DataGridTextColumn Header="Title" Binding="{Binding Title.Value}" CellStyle="{StaticResource TestStyle}" /> 
     <DataGridTextColumn Header="Body" Binding="{Binding Body.Value}" CellStyle="{StaticResource TestStyle}" /> 
    </DataGrid.Columns> 
</DataGrid> 

收集:

private ObservableCollection<Issue> mIssueList; 
public ObservableCollection<Issue> IssueList 
{ 
    get { return mIssueList; } 
    set { mIssueList = value; OnPropertyChanged("IssueList"); } 
} 

通過收集提前

回答

2

使用

public class Issue 
{ 
    public IssueElement Id { get; set; } 
    public IssueElement Title { get; set; } 
    public IssueElement Body { get; set; } 
} 

public class IssueElement 
{ 
    public string Value { get; set; } 
    public bool Updated { get; set; } 
} 

感謝班我不知道爲什麼你有e.Value,Two.Value,Three.Value in your bindings。我認爲你的意思是Id.Value,Title.Value和Body.Value,對嗎?

我認爲唯一可以做到這一點的方法是使用轉換器。 這裏是做這件事的一種方法:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource UpdatedConverter}}" Value="True"> 
    <Setter Property="Background" Value="Green" /> 
</DataTrigger> 

和轉換器:

public class UpdatedConverter : IValueConverter 
{ 
    #region IValueConverter Members 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     DataGridCell dgc = value as DataGridCell; 
     if (dgc != null) 
     { 
      Issue data = dgc.DataContext as Issue; 
      if (data != null) 
      { 
       DataGridTextColumn t = dgc.Column as DataGridTextColumn; 
       if (t != null) 
       { 
        var binding = t.Binding as System.Windows.Data.Binding; 
        if (binding != null && binding.Path != null && binding.Path.Path != null) 
        { 
         string val = binding.Path.Path.ToLower(); 
         if (val.StartsWith("id")) 
          return data.Id.Updated; 
         if (val.StartsWith("title")) 
          return data.Title.Updated; 
         if (val.StartsWith("body")) 
          return data.Body.Updated; 
        } 
       } 
      } 
     } 
     return false; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 

    #endregion 
} 
+0

謝謝,你說得對一二三,我忘了,在發佈前改變變量名。我會測試你的解決方案。 – Noxxys

+0

該解決方案的工作原理非常感謝! – Noxxys