2014-06-06 15 views
0

我試圖讓我的網格有點風格。我想根據每行中的信息對行進行着色。在這個操作中我有這個令人討厭的錯誤。而且這在應用程序啓動過程中發生。這是我在WPF中的第一步,我試圖做出一些有用的記錄器,但現在我撞上了巨大的牆壁。樣式DataGrid行WPF異常

錯誤: 在ItemsSource正在使用時,操作無效。改爲使用ItemsControl.ItemsSource訪問和修改元素。

值得一提的是,我使用靜態列表作爲ItemSource到網格。下面是我的一些代碼。

<DataGrid DockPanel.Dock="Bottom" Height="Auto" ItemsSource="{Binding Source={x:Static Log:Logger.LogCollection}, Mode=OneWay}" FontSize="12" FontFamily="Segoe UI"> 
      <i:Interaction.Behaviors> 
       <fw:ScrollGridView/> 
      </i:Interaction.Behaviors> 
      <Style TargetType="{x:Type DataGridRow}"> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding LogType}" Value="Info"> 
         <Setter Property="Background" Value="Red" /> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </DataGrid> 
    </DockPanel> 

和類

public static class Logger 
{ 
    private static ObservableCollection<LogMessage> _logCollection = new ObservableCollection<LogMessage>(); 

    public static ObservableCollection<LogMessage> LogCollection 
    { 
     get { return Logger._logCollection; } 
     set 
     { 
      if (_logCollection.Count > 500) 
      { 
       _logCollection.RemoveAt(_logCollection.Count - 1); 
      } 
      Logger._logCollection = value; 
     } 
    } 

    public static void Log(string message) 
    { 
     Log(LogType.Info, message, null); 
    } 

    public static void Log(LogType type, string message, string exception) 
    { 
     LogCollection.Add(new LogMessage { Type = type, Time = DateTime.Now, Message = message, Exception = exception }); 
    } 
} 

的LogMessage:

public enum LogType 
{ 
    Debug, 
    Warning, 
    Error, 
    Info 
} 

public class LogMessage 
{ 
    public LogType Type { get; set; } 
    public DateTime Time { get; set; } 
    public string Message { get; set; } 
    public string Exception { get; set; } 
} 

謝謝!

回答

0

確保在DataGrid.Columns定義欄目的DataGrid像下面,而不是直接內和DataGrid定義StyleDataGrid.Resource

 <DataGrid> 
      <DataGrid.Resources> 
       <Style TargetType="{x:Type DataGridRow}"> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding LogType}" Value="Info"> 
          <Setter Property="Background" Value="Red" /> 
         </DataTrigger> 
       </Style.Triggers> 
       </Style> 
      </DataGrid.Resources> 
      <DataGrid.Columns> 
       <DataGridTextColumn/> 
      </DataGrid.Columns> 

     </DataGrid> 
+0

它幫助感謝! – kjubersson

0

它幫助!現在看起來像這樣:

<DataGrid DockPanel.Dock="Bottom" Height="Auto" ItemsSource="{Binding Source={x:Static Log:Logger.LogCollection}, Mode=OneWay}" FontSize="12" FontFamily="Segoe UI" AutoGenerateColumns="False"> 
      <i:Interaction.Behaviors> 
       <fw:ScrollGridView/> 
      </i:Interaction.Behaviors> 
      <DataGrid.Resources> 
      <Style TargetType="{x:Type DataGridRow}"> 
       <Style.Triggers> 
         <DataTrigger Binding="{Binding Type}" Value="Info"> 
         <Setter Property="Background" Value="Red" /> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
      </DataGrid.Resources> 
      <DataGrid.Columns> 
       <DataGridTemplateColumn MinWidth="30" Header="Type"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <Image Width="16" 
           Height="16" 
           Source="{Binding Path=Type, 
               UpdateSourceTrigger=PropertyChanged, 
               Converter={StaticResource ImageTypeConverter}}" /> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
       <DataGridTextColumn Header="Time" 
            Binding="{Binding Time}"/> 
       <DataGridTextColumn Header="Message" 
            Binding="{Binding Message}" 
            Width="1200"> 
        <DataGridTextColumn.ElementStyle> 
         <Style> 
          <Setter Property="TextBlock.TextWrapping" Value="Wrap" /> 
         </Style> 
        </DataGridTextColumn.ElementStyle> 
       </DataGridTextColumn> 
       <DataGridTextColumn Header="Exception" 
            Binding="{Binding Exception}" 
            Width="1200"> 
        <DataGridTextColumn.ElementStyle> 
         <Style> 
          <Setter Property="TextBlock.TextWrapping" Value="Wrap" /> 
         </Style> 
        </DataGridTextColumn.ElementStyle> 
       </DataGridTextColumn> 
      </DataGrid.Columns> 
     </DataGrid> 
    </DockPanel> 

而且我還有一個問題。在將DataGrid.Resources完美添加到DataGrid.Resources之前,此事件停止觸發。這很奇怪,因爲我沒有改變添加項目到我的網格源的方式。

public class ScrollGridView : Behavior<DataGrid> 
{ 
    protected override void OnAttached() 
    { 
     base.OnAttached(); 
     this.AssociatedObject.SelectionChanged += new SelectionChangedEventHandler(AssociatedObject_SelectionChanged); 
    } 

    private void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     if (sender is DataGrid) 
     { 
      DataGrid grid = (sender as DataGrid); 
      if (grid.Items.Count > 0) 
      { 
       var border = VisualTreeHelper.GetChild(grid, 0) as Decorator; 
       if (border != null) 
       { 
        var scroll = border.Child as ScrollViewer; 
        if (scroll != null && !scroll.IsMouseOver) scroll.ScrollToEnd(); 
       } 
      } 
     } 
    } 

    protected override void OnDetaching() 
    { 
     base.OnDetaching(); 
     this.AssociatedObject.SelectionChanged -= new SelectionChangedEventHandler(AssociatedObject_SelectionChanged); 
    } 
}