2017-06-16 5 views
0

Dans mon application je dois manipuler un DataGrid dans le code-behind (Le DataGrid est également créé dans le code-behind en exécution), et je veux mettre en dessous des styles pour le DataGridComment interpréter le style par programme

<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> 

est en dessous du mon code interprété, mais je ne sais pas comment « traduire » la SelectiveScrollingGrid partie

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))); 

Répondre

0

Il suffit de créer un autre FrameworkElementFactory avec un type de 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); 
... 

Notez que la méthode recommandée pour créer par programme un modèle est de charger XAML d'un string ou un flux de mémoire en utilisant la méthode Load de la classe XamlReader comme indiqué dans le Documenation sur MSDN: https://msdn.microsoft.com/en-us/library/system.windows.frameworkelementfactory(v=vs.110).aspx

+0

remerciements pour vos commentaires, j'ai maintenant défini les styles en XAML et ensuite le charger en code-behind et c'est parfait pour moi! –