1

I have a simple single window WPF app using MVVM. XAML is binded to my MainViewModel.cs class, which contains an ObservableCollection Sites. Now SiteModel class has a property named Owners, which contains second ObservableCollection companies.

I need to bind my companies property to a ComboBox in my XAML. ComboBox is nested in DataGrid. Situation is described below.

<DataGrid Name="UxSiteGrid" DockPanel.Dock="Top" ItemsSource="{Binding Sites, Source={StaticResource ViewModel}}" AutoGenerateColumns="false" 
              Margin="5,5,5,0" CanUserAddRows="False" CanUserDeleteRows="True" CanUserResizeRows="False" CanUserReorderColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
            <DataGridTextColumn Binding="{Binding VseId}" Header="Vse ID"/>
            <DataGridTextColumn Binding="{Binding IMO}" Header="IMO"/>
            <DataGridComboBoxColumn SelectedItemBinding="{Binding SiteType}" ItemsSource="{local:EnumValues rdsConfigConnector:SiteType}" Header="Site type"/>
            <DataGridTextColumn Binding="{Binding TimePeriod}" Header="Fetching period"/>
            <DataGridTextColumn Binding="{Binding Latitude}" Header="Latitude"/>
            <DataGridTextColumn Binding="{Binding Longitude}" Header="Longitude"/>
            <DataGridTextColumn Binding="{Binding OffsetLatitude}" Header="Offset latitude"/>
            <DataGridTextColumn Binding="{Binding OffsetLongitude}" Header="Offset longitude"/>


            <DataGridTemplateColumn Header="Site Owners">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate >
                        <ComboBox ItemsSource="{Binding Owners.companies}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

        </DataGrid.Columns>
    </DataGrid>

1 Answer 1

0

The data-context inside your DataTemplate should already be your site so {Binding Owners.companies} should work (see e.g. here).
Be aware that data-binding is case-sensitive => may it shoudl be {Binding Owners.Companies}?

If that all is correct: Did you get a "binding error" in the output window?

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

3 Comments

Yes you are right about the case-sensitivity, it should be withe uppercas C. Still I got this error: System.Windows.Data Error: 40 : BindingExpression path error: 'Owners' property not found on 'object' ''MainViewModel' (HashCode=11497055)'. BindingExpression:Path=Owners.companies; DataItem='MainViewModel' (HashCode=11497055); target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable') Seems like data-context is on MainViewModel here
Hm, thats strange - I just checked in my project and there the DataTemplate is directly binding to my single item from my ItemsSource. Can you try creating a small test-app to just test that single binding feature?
Works fine in new project. I actually managed to fix it in my current app as well, seems like the lower case was a cause of trouble in the end. I feel a bit stupid right now :))

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.