2017-10-11 63 views
1

所有DataGridTextColumns我試圖做的是創建一個StyleDatagrid以應用在所有DataGridTextColumns一個自動換行沒有顯式地設置這樣的。風格通過AttachedProperty

<DataGrid ItemsSource="{Binding Lines}"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="Column1" Binding="{Binding Path=Result1}"> 
      <DataGridTextColumn.ElementStyle> 
       <Style TargetType="{x:Type TextBlock}"> 
        <Setter Property="TextWrapping" Value="Wrap"/> 
       </Style> 
      </DataGridTextColumn.ElementStyle> 
     </DataGridTextColumn> 
    </DataGrid.Columns> 
</DataGrid> 

遺憾的是無法使用一些Style如下直接,因爲DataGridTextColumn不是FrameworkElement

<Style TargetType="{x:Type TextBlock}" x:Key="WrapText"> 
    <Setter Property="TextWrapping" Value="Wrap"/> 
</Style> 

我發現這個解決辦法https://stackoverflow.com/a/2640862/5381620通過RayBurns,並試圖它是如何工作的弄清楚。但是,我是新來的附加屬性,因此不明白爲什麼它不起作用。

c#代碼似乎沒問題。

public class MyDataGridHelper : DependencyObject 
{ 
    private static readonly DependencyProperty TextColumnStyleProperty = DependencyProperty.RegisterAttached("TextColumnStyle", typeof(Style), typeof(MyDataGridHelper), new PropertyMetadata 
    { 
     PropertyChangedCallback = (obj, e) => 
     { 
      var grid = (DataGrid)obj; 
      if (e.OldValue == null && e.NewValue != null) 
       grid.Columns.CollectionChanged += (obj2, e2) => 
       { 
        UpdateColumnStyles(grid); 
       }; 
     } 
    }); 

    public static void SetTextColumnStyle(DependencyObject element, Style value) 
    { 
     element.SetValue(TextColumnStyleProperty, value); 
    } 
    public static Style GetTextColumnStyle(DependencyObject element) 
    { 
     return (Style)element.GetValue(TextColumnStyleProperty); 
    } 

    private static void UpdateColumnStyles(DataGrid grid) 
    { 
     var style = GetTextColumnStyle(grid); 
     foreach (var column in grid.Columns.OfType<DataGridTextColumn>()) 
      foreach (var setter in style.Setters.OfType<Setter>()) 
       if (setter.Value is BindingBase) 
        BindingOperations.SetBinding(column, setter.Property, (BindingBase)setter.Value); 
       else 
        column.SetValue(setter.Property, setter.Value); 
    } 
} 

我完全困惑的是當我們弄清楚風格二傳手。 目前我正在嘗試這種方式,這顯然不起作用,但實際上我不知道這個目標類型應該是什麼樣子。

<local:MyDataGridHelper.TextColumnStyle> 
    <Style TargetType="FrameworkElement"> 
     <Setter Property="TextBlock.TextWrapping" Value="Wrap"/> 
    </Style> 
</local:MyDataGridHelper.TextColumnStyle> 

回答

1

應設置列ElementStyle的附加屬性的值:

public class MyDataGridHelper : DependencyObject 
{ 
    private static readonly DependencyProperty TextColumnStyleProperty = 
     DependencyProperty.RegisterAttached("TextColumnStyle", typeof(Style), typeof(MyDataGridHelper), new PropertyMetadata 
    { 
     PropertyChangedCallback = (obj, e) => 
     { 
      var grid = (DataGrid)obj; 
      if (e.OldValue == null && e.NewValue != null) 
       grid.Columns.CollectionChanged += (obj2, e2) => 
       { 
        UpdateColumnStyles(grid); 
       }; 
     } 
    }); 

    public static void SetTextColumnStyle(DependencyObject element, Style value) 
    { 
     element.SetValue(TextColumnStyleProperty, value); 
    } 
    public static Style GetTextColumnStyle(DependencyObject element) 
    { 
     return (Style)element.GetValue(TextColumnStyleProperty); 
    } 

    private static void UpdateColumnStyles(DataGrid grid) 
    { 
     var style = GetTextColumnStyle(grid); 
     foreach (var column in grid.Columns.OfType<DataGridTextColumn>()) 
      column.ElementStyle = style; 
    } 
} 

用法:

<DataGrid> 
    <local:MyDataGridHelper.TextColumnStyle> 
     <Style TargetType="TextBlock"> 
      <Setter Property="Foreground" Value="Red"/> 
      <Setter Property="TextWrapping" Value="Wrap"/> 
     </Style> 
    </local:MyDataGridHelper.TextColumnStyle> 
    ... 
</DataGrid> 
+0

一個小的補充。這將覆蓋所有列的整個樣式。如果應該有一個專欄的個別信息,我們可以這樣做,只添加設置者。 (grid) var origStyle = GetTextColumnStyle(grid); ()中的var列。var newStyle = new Style(); var newStyle = new Style(); var newStyle = new Style(); newStyle.BasedOn = column.ElementStyle; newStyle.TargetType = origStyle.TargetType; (var setter in origStyle.Setters.OfType ()) { \t newStyle.Setters.Add(setter); } column.ElementStyle = newStyle; } } – pedrito