0

How using rotate transform in code behind to rotate transform header text in DataGrid on Silverlight? I don't have conception, but when I try using this code, is not working.

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

Any idea ?

4

1 回答 1

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;
        }
于 2013-09-19T08:59:20.300 回答