2017-06-16 52 views
0

在我的應用程序必須操縱代碼隱藏一個DataGrid(也是在運行時,後面的代碼創建的DataGrid中),我想設置如下樣式爲DataGrid如何解釋風格編程

<DataGrid.RowHeaderStyle> 
    <Style TargetType="DataGridRowHeader"> 
     <Setter Property="Visibility" Value="Collapsed"/> 
     <Setter Property="Template" Value="{x:Null}"/> 
    </Style> 
</DataGrid.RowHeaderStyle> 

<DataGrid.RowStyle> 
    <Style TargetType="DataGridRow"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="DataGridRow"> 
        <Border BorderThickness="{TemplateBinding Border.BorderThickness}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Name="DGR_Border" SnapsToDevicePixels="True"> 
         <SelectiveScrollingGrid> <!--How to translate this--> 
          <DataGridCellsPresenter ItemsPanel="{TemplateBinding ItemsControl.ItemsPanel}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"/> 
         </SelectiveScrollingGrid> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</DataGrid.RowStyle> 

下面是我的解釋代碼,但我不知道如何「翻譯」的SelectiveScrollingGrid部分

var myGrid = new DataGrid 
    { 
      RowHeaderStyle = new Style(typeof(DataGridRowHeader)), 
      RowStyle = new Style(typeof(DataGridRow)) 
    }; 

     myGrid.RowHeaderStyle.Setters.Add(new Setter(VisibilityProperty, Visibility.Collapsed)); 
     myGrid.RowHeaderStyle.Setters.Add(new Setter(DataGridRowHeader.TemplateProperty, null)); 

ControlTemplate templateButton = new ControlTemplate(typeof(DataGridRow)); 
     FrameworkElementFactory elemFactory = new FrameworkElementFactory(typeof(Border)); 
     elemFactory.SetValue(Border.BorderThicknessProperty, new TemplateBindingExtension(Border.BorderThicknessProperty)); 
     elemFactory.SetValue(Border.BorderBrushProperty, new TemplateBindingExtension(Border.BorderBrushProperty)); 
     elemFactory.SetValue(Border.BackgroundProperty, new TemplateBindingExtension(Panel.BackgroundProperty)); 
     elemFactory.SetValue(Border.NameProperty, "DGR_Border"); 
     elemFactory.SetValue(Border.SnapsToDevicePixelsProperty, true); 

     var cellsPresenterFactory = new FrameworkElementFactory(typeof(DataGridCellsPresenter)); 
     cellsPresenterFactory.SetValue(DataGridCellsPresenter.ItemsPanelProperty, new TemplateBindingExtension(ItemsControl.ItemsPanelProperty)); 
     cellsPresenterFactory.SetValue(DataGridCellsPresenter.SnapsToDevicePixelsProperty, new TemplateBindingExtension(UIElement.SnapsToDevicePixelsProperty)); 

     //elemFactory.AppendChild(selectiveScrollingGridFactory); 

     templateButton.VisualTree = elemFactory; 
     elemFactory.AppendChild(new FrameworkElementFactory(typeof(ContentPresenter))); 

回答

0

只需創建另一個FrameworkElementFactory有型的System.Windows.Controls.Primitives.SelectiveScrollingGrid

... 
var selectiveScrollingGridFactory = new FrameworkElementFactory(typeof(System.Windows.Controls.Primitives.SelectiveScrollingGrid)); 
elemFactory.AppendChild(selectiveScrollingGridFactory); 

var cellsPresenterFactory = new FrameworkElementFactory(typeof(DataGridCellsPresenter)); 
cellsPresenterFactory.SetValue(DataGridCellsPresenter.ItemsPanelProperty, new TemplateBindingExtension(ItemsControl.ItemsPanelProperty)); 
cellsPresenterFactory.SetValue(DataGridCellsPresenter.SnapsToDevicePixelsProperty, new TemplateBindingExtension(UIElement.SnapsToDevicePixelsProperty)); 

selectiveScrollingGridFactory.AppendChild(selectiveScrollingGridFactory); 
... 

注意,推薦的方法以編程方式創建的模板是從使用XamlReader類的Load方法的string或內存流加載XAML如documenation陳述上MSDN:https://msdn.microsoft.com/en-us/library/system.windows.frameworkelementfactory(v=vs.110).aspx

+0

感謝您的評論,我現在在XAML中定義了樣式,然後在代碼隱藏中加載它,這對我來說是完美的! –