2013-10-28 78 views

回答

2

我在這種事情的行爲中使用apllicationsettings並保存應用程序退出信息。

使用

<DataGrid> 
     <i:Interaction.Behaviors> 
      <local:DataGridBehavior GridSettings="{Binding Source={x:Static local:MySettings.Instance},Mode=OneWay}" /> 
     </i:Interaction.Behaviors> 
    </DataGrid> 

設置

​​

行爲

public class DataGridBehavior : Behavior<DataGrid> 
{ 

    public static readonly DependencyProperty GridSettingsProperty = 
     DependencyProperty.Register("GridSettings", typeof(IGridSettings), typeof(DataGridBehavior), null); 


    public IGridSettings GridSettings 
    { 
     get { return (IGridSettings)GetValue(GridSettingsProperty); } 
     set { SetValue(GridSettingsProperty, value); } 
    } 

    public DataGridICollectionViewSortMerkerBehavior() 
    { 
     Application.Current.Exit += CurrentExit; 
    } 

    private void CurrentExit(object sender, ExitEventArgs e) 
    { 
     SetSettings(); 
    } 

    protected override void OnAttached() 
    { 
     base.OnAttached(); 
     AssociatedObject.Loaded += AssociatedObjectLoaded; 
     AssociatedObject.Unloaded += AssociatedObjectUnloaded; 
    } 

    protected override void OnDetaching() 
    { 
     base.OnDetaching(); 
     AssociatedObject.Loaded -= AssociatedObjectLoaded; 
     AssociatedObject.Unloaded -= AssociatedObjectUnloaded; 
    } 

    private void AssociatedObjectUnloaded(object sender, RoutedEventArgs e) 
    { 
     SetSettings(); 
    } 

    void AssociatedObjectLoaded(object sender, RoutedEventArgs e) 
    { 
     var settings = GridSettings; 

     var columns = AssociatedObject.Columns.ToList(); 
     var colCount = columns.Count; 
     foreach (var column in columns) 
     { 
      var key = column.Header.ToString(); 

      if (settings.GridDisplayIndexList.ContainsKey(key)) 
      { 
       //manchmal wird -1 als index abgespeichert 
       var index = settings.GridDisplayIndexList[key]; 
       if(index > 0 && index < colCount) 
        column.DisplayIndex = index; 
      } 

      if (settings.GridColumnVisibilityList.ContainsKey(key)) 
      { 
       column.Visibility = settings.GridColumnVisibilityList[key]; 
      } 

      if (settings.GridColumnWidthList.ContainsKey(key)) 
      { 
       column.Width = new DataGridLength(settings.GridColumnWidthList[key]); 
      } 
     }   
    } 


    private void SetSettings() 
    { 
     var settings = GridSettings; 

     foreach (var column in AssociatedObject.Columns) 
     { 
      var key = column.Header.ToString(); 
      var displayindex = column.DisplayIndex; 
      var visibility = column.Visibility; 
      var width = column.ActualWidth; 

      if (settings.GridDisplayIndexList.ContainsKey(key)) 
      { 
       settings.GridDisplayIndexList[key] = displayindex; 
      } 
      else 
      { 
       settings.GridDisplayIndexList.Add(key, displayindex); 
      } 

      if (settings.GridColumnVisibilityList.ContainsKey(key)) 
      { 
       settings.GridColumnVisibilityList[key] = visibility; 
      } 
      else 
      { 
       settings.GridColumnVisibilityList.Add(key, visibility); 
      } 

      if (settings.GridColumnWidthList.ContainsKey(key)) 
      { 
       settings.GridColumnWidthList[key] = width; 
      } 
      else 
      { 
       settings.GridColumnWidthList.Add(key, width); 
      } 
     } 
    } 

} 
+0

感謝您的示例代碼,我會嘗試... !!! – neo