Silverlight DataGrid Header RotateTransform以编程方式

时间:2013-09-16 18:31:17

标签: c# silverlight datagrid rotation transform

如何在后面的代码中使用旋转变换来旋转Silverlight上的DataGrid中的变换头文本? 我没有概念,但是当我尝试使用这段代码时,它不起作用。

    private DataGridTemplateColumn CreateColumn(int index, string header)
    {
        string cellTemp = string.Format(@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" 
            xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
             <CheckBox />
        </DataTemplate>", index);

        DataGridTemplateColumn column = new DataGridTemplateColumn();
        column.Header = header;
        column.CellTemplate = (DataTemplate)XamlReader.Load(cellTemp);

        TransformGroup traGrp = new TransformGroup(){};
        traGrp.Children.Add(new RotateTransform(){ Angle = -35});

        Style transofrm = new Style(typeof(DataGridTemplateColumn));
        transofrm.Setters.Add(new Setter(TextBlock.RenderTransformProperty, traGrp));
        column.HeaderStyle = transofrm;

        return column;
    }

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

好的,你走了,

将此资源添加到您的用户控件

<UserControl.Resources>
        <Style TargetType="sdk:DataGridColumnHeader">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="sdk:DataGridColumnHeader">
                        <Canvas x:Name="RootElement" Height="60" HorizontalAlignment="Stretch">
                            <ContentPresenter Canvas.Left="15" Canvas.Top="50" Content="{TemplateBinding Content}">
                                <ContentPresenter.RenderTransform>
                                    <RotateTransform Angle="-90"/>
                                </ContentPresenter.RenderTransform>
                            </ContentPresenter>
                        </Canvas>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

并且在codebehind.cs中这样做,

private DataGridTemplateColumn CreateColumn(int index, string header)
        {
            string cellTemp = string.Format(@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" 
            xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
             <CheckBox />
             </DataTemplate>", index);
            DataGridTemplateColumn column = new DataGridTemplateColumn();
            column.Header = header;
            column.HeaderStyle = Resources["Template"] as Style;
            column.CellTemplate = (DataTemplate)XamlReader.Load(cellTemp);


            return column;
        }