This question is locked. New answers and comments are not allowed.
Perhaps I am simply misunderstanding the correct way to bind to a given cell in general, but here's my problem.
I have a grid full of cells of type StatisticCell which looks like this:
Which is bound dynamically on the client into a RadGridView using the DataTable class provided here.
With XAML:
My problem is that if I don't use a CellTemplate each cell correctly renders a StatisticCell.ToString() however when I bind, my BindingContext is always the dynamic row instead of the cell. For example an error in binding from above would be:
System.Windows.Data Error: BindingExpression path error: 'Value' property not found on 'DynamicObjectBuilder_a6f7b2a9-102e-4099-8aed-f404f55b601a' 'DynamicObjectBuilder_a6f7b2a9-102e-4099-8aed-f404f55b601a' (HashCode=18537444). BindingExpression: Path='Value' DataItem='DynamicObjectBuilder_a6f7b2a9-102e-4099-8aed-f404f55b601a' (HashCode=18537444); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..
How can I get the binding context to be the cell instead of the row? I had the exact same problem when I attempted to use the DictionaryConverter class provided in another thread. I assume I'm using either the wrong template or misunderstand how to index into the cell within the binding. Help!
I have a grid full of cells of type StatisticCell which looks like this:
[DataContract]public class StatisticCell{ [DataMember] public string Value { get; set; } [DataMember] public bool IsSelected { get; set; } public StatisticCell(string value, bool isSelected) { Value = value; IsSelected = isSelected; }}Which is bound dynamically on the client into a RadGridView using the DataTable class provided here.
using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.ComponentModel;using System.Windows.Data;using Telerik.Data;using Telerik.Windows.Controls;using DFA.Client.QueryToolService;using DFA.Client.ValueConverter;namespace DFA.Client.QueryTool.Control.Panes{ public partial class StatisticsContent : UserControl { public StatisticsContent() { InitializeComponent(); } public string Title { get { return "Statistics"; } } private DataTable _DataTable; public void LoadStatistics(ObservableCollection<StatisticRow> rows) { _DataTable = new DataTable(); foreach (var axis in rows.First().Data.Keys) { _DataTable.Columns.Add(new DataColumn() { ColumnName = axis, DataType = typeof(StatisticCell) }); } foreach (StatisticRow row in rows) { var dataRow = _DataTable.NewRow(); foreach (var axis in row.Data.Keys) { dataRow[axis] = row.Data[axis]; } _DataTable.Rows.Add(dataRow); } _StatisticsGridView.ItemsSource = _DataTable; } private void _StatisticsGridView_BeginningEdit(object sender, Telerik.Windows.Controls.GridViewBeginningEditRoutedEventArgs e) { } private void _ApplyButton_Click(object sender, RoutedEventArgs e) { Cursor = Cursors.Wait; } private void _ResetButton_Click(object sender, RoutedEventArgs e) { } private void _StatisticsGridView_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e) { e.Column.CellTemplate = (DataTemplate)Resources["_CellTemplate"]; } }}With XAML:
<UserControl x:Class="DFA.Client.QueryTool.Control.Panes.StatisticsContent" xmlns:converters="clr-namespace:DFA.Client.ValueConverter" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"> <UserControl.Resources> <converters:BooleanToBackgroundConverter x:Key="BooleanToBackgroundConverter"/> <DataTemplate x:Name="_CellTemplate" > <StackPanel Orientation="Horizontal" Background="Yellow" > <TextBlock Text="{Binding Path=Value}"> </TextBlock> </StackPanel> </DataTemplate> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White"> <telerik:RadGridView x:Name="_StatisticsGridView" CanUserReorderColumns="False" CanUserInsertRows="False" CanUserDeleteRows="False" CanUserFreezeColumns="False" ShowGroupPanel="False" ShowColumnHeaders="True" AutoGenerateColumns="True" CanUserSelect="False" BeginningEdit="_StatisticsGridView_BeginningEdit" Margin="0,0,12,49" SelectionUnit="Cell" IsFilteringAllowed="False" RowIndicatorVisibility="Collapsed" AutoGeneratingColumn="_StatisticsGridView_AutoGeneratingColumn"> </telerik:RadGridView> <Button Content="Apply" Height="23" HorizontalAlignment="Right" Margin="0,0,100,12" Name="_ApplyButton" VerticalAlignment="Bottom" Width="75" Click="_ApplyButton_Click" IsEnabled="{Binding IsDirty}" /> <Button Content="Reset" Height="23" HorizontalAlignment="Right" Margin="0,0,12,12" Name="_ResetButton" VerticalAlignment="Bottom" Width="75" Click="_ResetButton_Click" IsEnabled="{Binding IsDirty}" /> </Grid></UserControl>My problem is that if I don't use a CellTemplate each cell correctly renders a StatisticCell.ToString() however when I bind, my BindingContext is always the dynamic row instead of the cell. For example an error in binding from above would be:
System.Windows.Data Error: BindingExpression path error: 'Value' property not found on 'DynamicObjectBuilder_a6f7b2a9-102e-4099-8aed-f404f55b601a' 'DynamicObjectBuilder_a6f7b2a9-102e-4099-8aed-f404f55b601a' (HashCode=18537444). BindingExpression: Path='Value' DataItem='DynamicObjectBuilder_a6f7b2a9-102e-4099-8aed-f404f55b601a' (HashCode=18537444); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..
How can I get the binding context to be the cell instead of the row? I had the exact same problem when I attempted to use the DictionaryConverter class provided in another thread. I assume I'm using either the wrong template or misunderstand how to index into the cell within the binding. Help!
