0

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;
                // }
            }
1
  • Could you share the code of setting the DataContext of local:TotalCellTemplateSelector ? I guess it could be the problem that you set the wrong DataContext or as @ChrisF mentioned you have got a typo. Commented Jan 9, 2016 at 4:14

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

I tried but no luck . :(. I get this error this time:
BindingExpression path error: 'ChargeCodeValidValues' property not found on 'object' ''DataRowView' (HashCode=18770399)'. BindingExpression:Path=DataContext.RowData.Row.ChargeCodeValidValues; DataItem='ComboBox' (Name=''); target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')
BindingExpression path error: 'oChargeCodesValidvalues' property not found on 'object' ''DataRowView' (HashCode=44637535)'. BindingExpression:Path=DataContext.RowData.Row.oChargeCodesValidvalues; DataItem='ComboBox' (Name=''); target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')
Again, after doing some guesswork I come to a conclusion that you're using a DataTable as the items source for the grid. At this point we can't go any further unless you post some information (preferably code) on how you build the DataTable and what is ChargeCodeValidValues (if it is a property, a class definition with relevant parts would be great). Also, I think it would be helpful if you showed us how you would access the data in question in code-behind (how does the ChargeCodeValidValues relate to the DataTable).
Thanks for looking at it.I will edit my post to provide some more info.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.