Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 671275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:21:20+00:00 2026-05-14T00:21:20+00:00

New to wpf and therefore struggling a bit. I am putting together a quick

  • 0

New to wpf and therefore struggling a bit.
I am putting together a quick demo before we go for the full implementation
I have a treeview on the left with

Continent
    Country
       City  structure

when a user select the city it should populate some textboxes in a tabcontrol on the right hand side

I made it sort of work but cannot make it work with composite objects.

In a nutshell can you spot what is wrong with my zaml or code.
Why is not binding to a my CityDetails.ClubsCount or CityDetails.PubsCount?

What I am building is based on http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx

Thanks a lot for any suggestions or reply

DataModel

public class City
{
    public City(string cityName)
    {
        CityName = cityName;
    }

    public string CityName { get; set; }
    public string Population { get; set; }
    public string Area { get; set; }
    public CityDetails CityDetailsInfo { get; set; }
}

public class CityDetails
{
    public CityDetails(int pubsCount,int clubsCount)
    {
        PubsCount = pubsCount;
        ClubsCount = clubsCount;
    }

    public int ClubsCount { get; set; }
    public int PubsCount { get; set; }
}

ViewModel

public class CityViewModel : TreeViewItemViewModel
{
    private  City _city;
    private RelayCommand _testCommand;
    public CityViewModel(City city, CountryViewModel countryViewModel):base(countryViewModel,false)
    {
        _city = city;
    }

    public string CityName
    {
        get { return _city.CityName; }
    }
    public string Area
    {
        get { return _city.Area; }
    }
    public string Population
    {
        get { return _city.Population; }
    }
    public City City
    {
    get { return _city; }
    set { _city = value; }
    }

    public CityDetails CityDetailsInfo
    {
        get { return _city.CityDetailsInfo; }
        set { _city.CityDetailsInfo = value; }
    }
}

XAML

<DockPanel>
    <DockPanel LastChildFill="True">
        <Label DockPanel.Dock="top" Content="Title " HorizontalAlignment="Center"></Label>
        <StatusBar DockPanel.Dock="Bottom">
            <StatusBarItem Content="Status Bar" ></StatusBarItem>
        </StatusBar>
        <Grid DockPanel.Dock="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="2*"/>
            </Grid.ColumnDefinitions>
            <TreeView Name="tree" ItemsSource="{Binding Continents}">
                <TreeView.ItemContainerStyle>
                    <Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="IsExpanded" Value="{Binding IsExpanded,Mode=TwoWay}"/>
                        <Setter Property="IsSelected" Value="{Binding IsSelected,Mode=TwoWay}"/>
                        <Setter Property="FontWeight" Value="Normal"/>
                        <Style.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="FontWeight" Value="Bold"></Setter>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </TreeView.ItemContainerStyle>

                <TreeView.Resources>
                    <HierarchicalDataTemplate DataType="{x:Type ViewModels:ContinentViewModel}"
                                      ItemsSource="{Binding Children}">
                        <StackPanel Orientation="Horizontal">
                            <Image Width="16" Height="16" Margin="3,0" Source="Images\Continent.png"/>
                            <TextBlock Text="{Binding ContinentName}"/>
                        </StackPanel>
                    </HierarchicalDataTemplate>

                    <HierarchicalDataTemplate DataType="{x:Type ViewModels:CountryViewModel}"
                                      ItemsSource="{Binding Children}">
                        <StackPanel Orientation="Horizontal">
                            <Image Width="16" Height="16" Margin="3,0" Source="Images\Country.png"/>
                            <TextBlock Text="{Binding CountryName}"/>
                        </StackPanel>
                    </HierarchicalDataTemplate>
                    <DataTemplate DataType="{x:Type ViewModels:CityViewModel}" >
                        <StackPanel Orientation="Horizontal">
                            <Image Width="16" Height="16" Margin="3,0" Source="Images\City.png"/>
                            <TextBlock Text="{Binding CityName}"/>
                        </StackPanel>
                    </DataTemplate>
                </TreeView.Resources>
            </TreeView>
            <GridSplitter Grid.Row="0" Grid.Column="1" Background="LightGray"
                 Width="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
            <Grid Grid.Column="2" Margin="5" >
                <TabControl>                                
                    <TabItem Header="Details" DataContext="{Binding Path=SelectedItem.City, ElementName=tree, Mode=OneWay}">
                        <StackPanel >
                            <TextBlock VerticalAlignment="Center" FontSize="12" Text="{Binding CityName}"/>
                            <TextBlock VerticalAlignment="Center" FontSize="12" Text="{Binding Area}"/>
                            <TextBlock VerticalAlignment="Center" FontSize="12" Text="{Binding Population}"/>
                            <!-- DONT WORK WHY-->
                            <TextBlock VerticalAlignment="Center" FontSize="12" Text="{Binding SelectedItem.CityDetailsInfo.ClubsCount}"/>
                            <TextBlock VerticalAlignment="Center" FontSize="12" Text="{Binding  SelectedItem.CityDetailsInfo.PubsCount}"/>
                        </StackPanel>
                    </TabItem>
                </TabControl>
            </Grid>
        </Grid>
    </DockPanel>       
</DockPanel>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:21:20+00:00Added an answer on May 14, 2026 at 12:21 am

    Look here (irrelevant bits elided):

    <TabItem DataContext="{Binding Path=SelectedItem.City, ElementName=tree, Mode=OneWay}">
      <StackPanel >
        <!-- DONT WORK WHY-->
        <TextBlock Text="{Binding SelectedItem.CityDetailsInfo.ClubsCount}"/>
        <TextBlock Text="{Binding SelectedItem.CityDetailsInfo.PubsCount}" />
      </StackPanel>
    </TabItem>
    

    The DataContext for the TabItem is the SelectedItem.City: that is, the DataContext is a City. City doesn’t have a SelectedItem property. Change your bindings to e.g.

    <TextBlock Text="{Binding CityDetailsInfo.ClubsCount}" />
    

    i.e. start the path from the current DataContext, in this case the City object.

    As a tip, the Visual Studio Output window displays binding errors, which can often provide sufficient information to diagnose this immediately e.g. if you see “Property ‘SelectedItem’ not found on ‘City'” it’s clear that WPF isn’t looking at the object you think it’s looking at!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to WPF and therefore this might be a stupid question, but I
New to wpf and through a learning curve. I have a userControl with a
I'm new to WPF. I have a WPF Window with a bunch of Labels
I am new to WPF. I have a ListBox that has its ItemSource set
I have created a new WPF Application using Visual Studio 2010 so I have
I have tried to play with the new WPF Ribbon control available in the
We have an old (Win32) and new (WPF) version of our blotter software, which
I have created a new WPF application and added an event handler for Loaded
I'm BRAND NEW to the whole C#/WPF thing. I have a decent understanding of
While designing a new WPF application I noticed exceptions not being thrown during data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.