简体   繁体   中英

why property of actual class in ObservableCollection is not found for DataGridTextcolumn but parent class property is?

I get:

System.Windows.Data Error: 40 : BindingExpression path error: 'State' property not found on 'object' ''PointNetObject' (HashCode=9270846)'. BindingExpression:Path=State; DataItem='PointNetObject' (HashCode=9270846); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

for one column. All the rows are added to table, but for each row i get the error. Two other columns are displayed just fine for each row. But not the State. Naturally the converter is never called.

Set-up described below.

I have an object:

public class PointNetObject : NetObject
{
    SwitchObjectState State
    {
        get { return _state; }
        set { _state = value; }
    }
}

which inherits class that has properties like Phase and Label

In ViewModel:

public ObservableCollection<PointNetObject> SelectedSwitchItems { get; private set; }

public SelectedObjectsViewModel(SelectedObjects selectedObjects)
{
    SelectedSwitchItems = new ObservableCollection<PointNetObject>(GetSwitches());
}    


IEnumerable<PointNetObject> GetSwitches()
{
    foreach (var netObject in SelectedObjectsInstance.GetSelectedObjectItems(x => IsSwitch(x)))
    {
        yield return (PointNetObject) netObject;
    }
}

in View:

 <DataGrid Name="SelectedSwitchesGrid" CanUserAddRows="False" CanUserResizeColumns="True" CanUserSortColumns="True" IsReadOnly="True" 
                   ItemsSource="{Binding SelectedSwitchItems}"
           <DataGrid.Columns>
                    <DataGridTextColumn Header="{DynamicResource XpStrLabel}" Binding="{Binding Label}" />
                    <DataGridTextColumn Header="{DynamicResource XpStrPhase}" Binding="{Binding Phase}" />
                    <DataGridTextColumn  Header="{DynamicResource XpStrState}" Binding="{Binding State, Converter={StaticResource SwitchObjectStateToStringConverter}}" />
                </DataGrid.Columns>
  </DataGrid>

In your model:

public class PointNetObject : NetObject
{
    SwitchObjectState State
    {
       get { return _state; }
       set { _state = value; }
    }
}

You State property isn't declared as public.

Any properties you are binding to from the ViewModel to the View need to be public, or the View won't be able to see it.

Don't worry, we've all done it once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM