如何以编程方式解释样式
How to interpret the style programmatically
在我的应用程序中,我必须在代码隐藏中操作 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>
下面是我解释的代码,但我不知道如何 'translate' 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)));
只需创建另一个类型为 System.Windows.Controls.Primitives.SelectiveScrollingGrid
的 FrameworkElementFactory
:
...
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 class 如 MSDN 上的文档所述:https://msdn.microsoft.com/en-us/library/system.windows.frameworkelementfactory(v=vs.110).aspx
在我的应用程序中,我必须在代码隐藏中操作 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>
下面是我解释的代码,但我不知道如何 'translate' 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)));
只需创建另一个类型为 System.Windows.Controls.Primitives.SelectiveScrollingGrid
的 FrameworkElementFactory
:
...
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 class 如 MSDN 上的文档所述:https://msdn.microsoft.com/en-us/library/system.windows.frameworkelementfactory(v=vs.110).aspx