views:

18

answers:

2

Hello First sorry for my english. I have started recently my first project on wpf. I´m styling a custom DataGrid who have been defined programatically (the xaml code doesn´t exists). I have styled all that i need in my datagrid control except a checkbox that i wrapped inside. The problem is that in other place of my application i defined a checkbox style how are applying correctly but i can´t apply inside my datagrid. Actually my datagrid doesn´t throw syntax errors but when the datagrid runs the checkbox styles doesn´t apply. The style code look like this (its defined in a stylesheet)

... <Setter Property="DataGridCheckBoxColumn.ElementStyle">
        <Setter.Value>
            <Style TargetType="{x:Type CheckBox}">

                <Setter Property="Background" Value="Yellow"/>
                <Setter Property="BorderBrush" Value="{DynamicResource NormalBorderBrush}"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type CheckBox}">


                            <BulletDecorator Background="Transparent">
                                <BulletDecorator.Bullet>
                                    <Grid Width="13" Height="13">
                                        <Border x:Name="Border" Background="Pink" BorderBrush="Black" BorderThickness="1,1,1,1" CornerRadius="2,2,2,2"/>
                                        <Path x:Name="CheckMark" Stroke="Green" StrokeThickness="2" SnapsToDevicePixels="False" Data="M1.5000001,1.5833334 L9.7920001,9.6666667 M1.5420001,9.6666667 L9.7083333,1.5000001" Margin="1" ClipToBounds="False" StrokeEndLineCap="Round" StrokeStartLineCap="Round"/>
                                    </Grid>
                                </BulletDecorator.Bullet>
                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
                            </BulletDecorator>

                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>

    </Setter>...

Its exactly the same that it`s applying in the apliccation. I´ve read a lot about it but i can´t to apply it, i tried, also, setting the setter property to "DatagridBoundColum.ElementStyle" and also to "CellStyle" but it doesn´t work. Any suggest?? Thank a lot.

A: 

Do it like you would do in xaml:

<UserControl.Resources>
    <DataTemplate x:Key="CheckBoxTemplate">
        <CheckBox Style="{StaticResource AnyResourceKeyInApplciation}"/>
    </DataTemplate>
</UserControl.Resources>
<DataGrid x:Name="dataGrid" />

this.dataGrid.Columns.Add(new DataGridTemplateColumn{
    CellTemplate=this.Resources["CheckBoxTemplate"] as DataTemplate});
vorrtex
A: 

Thanks for your Reply vorrtex.

I didn´t apply it exactly but it helped me to find the solution, however i would have liked not to modify the VB code and only to modify it the xaml style tag.

I find an object how simplify this task. The syntax it´s the following:

column2.ElementStyle = Application.Current.FindResource("CheckBoxStyle")

It´s applying style ok inside the datagrid. But actually it´s placing at left border of the cell. I´ll try to find why.

Thanks again.

Ramos