Home Forums WPF controls Xceed DataGrid for WPF Create a template that behaves identical to the default one

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #25339 |

    Create a template that behaves identical to the default one

    Hello.

    I have a grid in that I want to customize a few details of one column’s cells (add a DataTrigger to set a property). But for all other things, I want them to behave exactly the same as if i don’t give any CellContent or CellEditor template.

    I have currently reached up to this code:

    <DataTemplate x:Key=”TestCellContentTemplate”>
    <xcdg:AutoSelectTextBox x:Name=”TestText” Margin=”0″ Padding=”0″ BorderThickness=”0″ Text=”{Binding Path=.}” />
    </DataTemplate>

    <DataTemplate x:Key=”TestCellEditorTemplate”>
    <xcdg:AutoSelectTextBox x:Name=”TestText” Margin=”0″ Padding=”0″ BorderThickness=”0″ Text=”{xcdg:CellEditorBinding}”/>
    </DataTemplate>

    (…)

    <xcdg:DataGridControl x:Name=”TestGrid” AutoCreateColumns=”False” ItemsSource=”{Binding Source={StaticResource TestSource}}”>
    <xcdg:DataGridControl.Columns>
    <xcdg:Column Title=”Column 1″ FieldName=”Column1″ VisiblePosition=”0″ />
    <xcdg:Column Title=”Column 2″ FieldName=”Column2″ VisiblePosition=”1″ />
    <xcdg:Column Title=”Column 3″ FieldName=”Column3″ VisiblePosition=”2″
    CellContentTemplate=”{StaticResource TestCellContentTemplate}”>
    <xcdg:Column.CellEditor>
    <xcdg:CellEditor EditTemplate=”{StaticResource TestCellEditorTemplate}” />
    </xcdg:Column.CellEditor>
    </xcdg:Column>
    </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl>

    I have two problems:

    1) The cell content appears always in black text on white background, although the cell itself changes it colors when the cursor moves and row and cell selection moves.
    2) If I click the mouse into the cell, it begins edit mode, but its row is not selected! Thus, I have one row selected (with blue cursor) while I am editing a cell of other row. Plus the Enter and ESC keys don’t work.

    Is there a solution for this? Thanks.

    Imported from legacy forums. Posted by Andrés (had 4068 views)

    User (Old forums)
    Member
    Post count: 23064

    Other way I have tried is to use a Style to change the cells, without using templates:

    <Style TargetType=”{x:Type xcdg:DataCell}”>
    <Style.Triggers>
    <DataTrigger Binding=”{Binding Path=.}” Value=”Yes”>
    <Setter Property=”FontSize” Value=”15″/>
    <Setter Property=”Background” Value=”Red”/>
    </DataTrigger>
    </Style.Triggers>
    </Style>

    But I don’t know how to apply this style only to cells belonging to a particular column.

    Also, I can’t reach the value of the data inside the cell (the binding i put above do not work).

    Imported from legacy forums. Posted by Andrés (had 509 views)

    User (Old forums)
    Member
    Post count: 23064

    Please can someone take a look at this? It’s not a critical question but is a problem that I am carrying since the beginning of my project four months ago, and affects several screens of my application.

    I think this is a rather common need and should be easy to do, but I’m not able to find a way.

    Imported from legacy forums. Posted by Andrés (had 351 views)

    User (Old forums)
    Member
    Post count: 23064

    Finally I found a way by myself. I discovered that the cell’s context is the corresponding item of the items source (in my case a DataRow), and I learned to use RelativeSource:

    <Style TargetType=”{x:Type xcdg:DataCell}”>
    <Style.Triggers>
    <MultiDataTrigger>
    <MultiDataTrigger.Conditions>
    <Condition Binding=”{Binding RelativeSource={x:Static RelativeSource.Self},Path=FieldName}” Value=”MyColumn”/>
    <Condition Binding=”{Binding Path=MyColumn}” Value=”Yes”/>
    </MultiDataTrigger.Conditions>
    <Setter Property=”FontSize” Value=”15″/>
    <Setter Property=”Background” Value=”Red”/>
    </MultiDataTrigger>
    </Style.Triggers>
    </Style>

    This seems to work ok, although I think that it’s a little awkward. It would be very nice if there existed a property “CellStyle” in the Column class, similar to CellContentTemplate or CellErrorStyle.

    Imported from legacy forums. Posted by Andrés (had 1001 views)

    User (Old forums)
    Member
    Post count: 23064

    The cell does have a property CellContentTemplate, that should you what you want.
    http://doc.xceedsoft.com/products/XceedWpfDataGrid/Xceed.Wpf.DataGrid~Xceed.Wpf.DataGrid.Column~CellContentTemplate.html

    Imported from legacy forums. Posted by Frances (had 457 views)

    User (Old forums)
    Member
    Post count: 23064

    Eureka!!!

    I have finally found the cause of the problems of my first attempt. I was writing a CellContentTemplate for the cell, and I was putting an editable control (an AutoSelectTextBox). And this is wrong, because the content template is only for showing the data. When I want to edit, the CellEditor is used.

    So, changing it to a TextBlock, all works. This is the result:

    <DataTemplate x:Key=”TestCellContentTemplate”>
    <TextBlock x:Name=”TestText” Text=”{Binding}” />
    <DataTemplate.Triggers>
    <DataTrigger Binding=”{Binding Path=.}” Value=”MyValue”>
    <Setter TargetName=”TestText” Property=”Background” Value=”Green”/>
    </DataTrigger>
    </DataTemplate.Triggers>
    </DataTemplate>

    (…)

    <xcdg:DataGridControl x:Name=”TestGrid” AutoCreateColumns=”False” ItemsSource=”{Binding Source={StaticResource TestSource}}”>
    <xcdg:DataGridControl.Columns>
    <xcdg:Column Title=”Column 1″ FieldName=”Column1″ VisiblePosition=”0″ />
    <xcdg:Column Title=”Column 2″ FieldName=”Column2″ VisiblePosition=”1″ />
    <xcdg:Column Title=”Column 3″ FieldName=”Column3″ VisiblePosition=”2″
    CellContentTemplate=”{StaticResource TestCellContentTemplate}”>
    </xcdg:Column>
    </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl>

    This has only a small problem. The cell has a padding of 3 pixels, and inside it there is the CellContent. If I change the background of the content, I can see a white border 3 pixels wide around the background. The only solution is to change the background of the cell itself, and for this, I use the solution in my last post, with a MultiDataTrigger.

    Hope it helps to anybody else 😉

    Imported from legacy forums. Posted by Andrés (had 5624 views)

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.