Hi, I have XamGrid with binding to the DataTable. This is because i need to create columns dynamically. Each cell should contain individual class and i need access to its properties that are different for each cell except for one property that is same in one row and one property that is same in one column. Example: The different properties for each cell are Name and Value, Date is same for each row, Type is same for each column.
I used XamDataGrid before and to get value of single cell i used this (there are more textboxes in a single cell):
<Style TargetType="{x:Type igDP:CellValuePresenter}" x:Key="cellTemplate" x:Name="cellTemplate" > <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <Border> <Border.ToolTip> <StackPanel> <TextBlock Text="Database Value:"/> <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value.DBValue}"/> </StackPanel> </Border.ToolTip> <StackPanel Orientation="Vertical" VerticalAlignment="Center"> <TextBlock Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type igDP:CellValuePresenter}}, Path=Value.DateHolder, Converter={StaticResource VisibilityConverter}}" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type igDP:CellValuePresenter}}, Path=Value.Date}" /> </StackPanel> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <igDP:XamDataGrid Grid.Row="1" DataSource="{Binding Path=ImportData.DefaultView}" ScrollingMode="Immediate" AutoFit="True" GroupByAreaLocation="None"> <igDP:XamDataGrid.FieldSettings> <igDP:FieldSettings CellValuePresenterStyle="{StaticResource cellTemplate}" CellClickAction="SelectCell" /> </igDP:XamDataGrid.FieldSettings> </igDP:XamDataGrid>
<Style TargetType="{x:Type igDP:CellValuePresenter}" x:Key="cellTemplate" x:Name="cellTemplate" > <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <Border> <Border.ToolTip> <StackPanel> <TextBlock Text="Database Value:"/> <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value.DBValue}"/> </StackPanel> </Border.ToolTip> <StackPanel Orientation="Vertical" VerticalAlignment="Center"> <TextBlock Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type igDP:CellValuePresenter}}, Path=Value.DateHolder, Converter={StaticResource VisibilityConverter}}" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type igDP:CellValuePresenter}}, Path=Value.Date}" /> </StackPanel> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>
<igDP:XamDataGrid Grid.Row="1" DataSource="{Binding Path=ImportData.DefaultView}" ScrollingMode="Immediate" AutoFit="True" GroupByAreaLocation="None"> <igDP:XamDataGrid.FieldSettings> <igDP:FieldSettings CellValuePresenterStyle="{StaticResource cellTemplate}" CellClickAction="SelectCell" /> </igDP:XamDataGrid.FieldSettings> </igDP:XamDataGrid>
Is something like this possible with XamGrid? I tried this but i get whole row in converter and i don't know how to address individual cell in that row.
<Style x:Key="CellStyle" TargetType="ig:CellControl"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ig:CellControl"> <TextBlock Text="{Binding Converter={StaticResource converter}}" /> </ControlTemplate> </Setter.Value> </Setter> </Style> <ig:XamGrid Grid.Row="2" ItemsSource="{Binding Path=ImportData.DefaultView}" CellStyle="{StaticResource CellStyle}"></ig:XamGrid>
<Style x:Key="CellStyle" TargetType="ig:CellControl"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ig:CellControl"> <TextBlock Text="{Binding Converter={StaticResource converter}}" /> </ControlTemplate> </Setter.Value> </Setter> </Style>
<ig:XamGrid Grid.Row="2" ItemsSource="{Binding Path=ImportData.DefaultView}" CellStyle="{StaticResource CellStyle}"></ig:XamGrid>
Reason for changing XamDataGrid for XamGrid is selection possibilities with XamGrid. In XamDataGrid I can't select individual cells by clicking and draging like in XamGrid. I am using MVVM so events may be problem. It would be better for me to use converters and bindings, triggers, styles etc. Source data don't have to be in form of DataTable. I can load it to almost everything (arrays, lists, expandoObjects,...). Thank you for any advice.
Lukas
Hi Lukas, Thank you for your post.
I have been looking into through it and in your scenario, to get the value of the cell control you can create RelatveSource Bindin to CellCotrol and you can access the value of the cell through the Cell property of CellControl. I have created a sample application for you, based on the code that you have provided, that shows how you can use the XamGrid in this scenario.
Feel free to write me if you need further assistance on this matter.
Thank you, it works like a charm.