Saru Saru - 3 months ago 35
C# Question

WPF binding errors

I am new to WPF and this binding error is making me crazy.
I have a datatemplate which has combobox. That datatemplate is used by gridview column. I am trying to bind the combobox to csla object but it is throwing me below error:


System.Windows.Data Error: 40 : BindingExpression path error:

'oChargeCodesValidvalues' property not found on 'object'
''EditGridCellData' (HashCode=59067897)'.
BindingExpression:Path=DataContext.oChargeCodesValidvalues;
DataItem='ComboBox' (Name=''); target element is 'ComboBox' (Name='');
target property is 'ItemsSource' (type 'IEnumerable')


here is the snippet of my xaml:

<DataTemplate x:Key="combodescriptionTemplate">
<ComboBox Name="cboCodeValidValues" ItemsSource="{Binding DataContext.oChargeCodesValidvalues, RelativeSource={RelativeSource Mode=Self}}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Description}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
<local:TotalCellTemplateSelector x:Key="totalcellTemplateSelector"
combodescriptionTemplate="{StaticResource combodescriptionTemplate}"/>


Some more info based on the comments:
I am using a devexpress gridview in wcf. There are 2 columns both bounded to field. First is combobox and second is textbox by default and combo based on value of first column.The gridview datasource is code csla object. ChargeCodeValidValues is cslaobjcet . And i used datatemplate to change editor of second column.

Below is the code snippet of my ChargeCodeValidValues object:

public class ChargeCodeValidValues
{


public DataTable ChargeCodesValidValuesTable { get; set; }




public ChargeCodeValidValues()
{
LoadChargeCodesValidValues();
}

public void LoadChargeCodesValidValues()
{
//get data from db
}


calling in code:

private ChargeCodeValidValues oChargeCodesValidvalues;
oChargeCodesValidvalues = new ChargeCodeValidValues();


and i am trying to bind combox to oChargeCodesValidvalues and getting above error.

Here is the snippet of my TotalCellTemplateSelector:

public DataTemplate combodescriptionTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
GridCellData cellData = item as GridCellData;


if (cellData != null)
{

RowData rowdata = cellData.RowData;

DataRowView rowview = rowdata.Row as DataRowView;
if (rowview != null)
{
DataRow drCC = rowview.Row;
//if(drCC.Field<int>("FieldName") != DBNull.Value)
//{
int Code = drCC.Field<int>("FieldName", 0);
if (Code == Value)
return combodescriptionTemplate;
else

return null;
// }
}

Answer

If I'm not mistaken youre using a GridControl (or TreeListControl) from DevExpress WPF controls suite and trying to template a cell.

For each cell the data that is supposed to be presented is wrapped in an EditGridCellData object, and to this object the data template is applied (thus it is by default the value of DataContext property for controls inside the template). If the column is in bound mode (that is either Binding or FieldName is set) the value produced by the binding for current row is exposed by EditGridCellData.Value property. You can also access the object associated with current row through EditGridCellData.RowData.Row path.

Having said that and assuming oChargeCodesValidvalues is a property of the object (item) associated with current row, you should change your binding to:

{Binding DataContext.RowData.Row.oChargeCodesValidvalues, RelativeSource={RelativeSource Mode=Self}}

As a side note, if you don't explicitly specify the source for the binding, the DataContext of the target object is automatically used* as source, so you could shorten your syntax to:

{Binding RowData.Row.oChargeCodesValidvalues}

* provided the target object is a FrameworkElement or a FrameworkContentElement