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 9199799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:37:06+00:00 2026-06-17T22:37:06+00:00

I am binding a model to my Xaml code and have a question about

  • 0

I am binding a model to my Xaml code and have a question about how to bind to a Property.

Let’s assume my View Model looks like

internal class LogsVM
{
    private List<Log> logList;
    public List<Log> LogList
    {
        get; set;
    }       

    public LogsVM()
    {

    }

    public LogsVM(List<Logging.Log> logs)
    {
        logList = logs;
    }
}

and assume my Log class looks like

internal class Log
{
    public string Title { get;set; }
    public List<MoreDetails> moreDetails;

    public Log()
    {
        moreDetails= new List<MoreDetails>();
    }
}

In Xaml, how do I bind to the Title within a TreeView?

My Xaml so far looks like

<Window x:Class="BackUps.Logging.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:myData ="clr-namespace:BackUps.Logging.ViewModel"
        Title="Logging Results" Height="350" Width="525">

    <Grid>
        <Grid.Resources>
            <myData:LogsVM x:Key="Vm" />
        </Grid.Resources>
        <Grid.DataContext>
            <Binding Source="{StaticResource Vm}"></Binding>
        </Grid.DataContext>
        <TreeView>
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate DataType="{x:Type myData:LogsVM}" ItemsSource="{Binding LogList}">
                    <TextBlock Text="{Binding Title}" />
                    <HierarchicalDataTemplate.ItemTemplate>
                        <DataTemplate DataType="{x:Type myData:LogsVM}">
                            <TextBlock Text="{Binding moreDetails.Staus}" />
                        </DataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</Window>

And my MainWindow code behind

public MainWindow(List<Log> logs)
    {
        InitializeComponent();
        LogsVM logVm = new LogsVM(logs);
        this.DataContext = logVm;
    }

As you can see in the above code, I’m trying to bind the Title property but my screen doesn’t display an text at all.

So, my 2 questions are:

  1. Is it enough to use my ViewModel class alone or do I also need to tell the Xaml each internal class of the ViewModel (in this case, the Log class)? EG

    xmlns:myData =”clr-namespace:BackUps.Logging.ViewModel”
    xmlns:moreData = “clr-namespace:BackUps.Logging.Logs”

  2. What do I need to do to bind the Title?

  • 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-06-17T22:37:07+00:00Added an answer on June 17, 2026 at 10:37 pm

    Binding is not complicated as you might think, Your are just not mastering the Treeview‘s HierarchicalDataTemplate stuff and exposing properties to the XAML,
    set your all your domain classes public cause they are used in public properties.
    myData should reference the domain classes namespace.for ex: in my case xmlns:myData="clr-namespace:WpfApplication3" MoreDetails has to be a public property in Log class.

       <TreeView ItemsSource="{Binding LogList}" >
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate DataType="{x:Type myData:Log}" ItemsSource="{Binding MoreDetails}">
                    <TextBlock Text="{Binding Title}" />
                    <HierarchicalDataTemplate.ItemTemplate >
                        <DataTemplate DataType="{x:Type myData:MoreDetails}" >
                            <TextBlock Text="{Binding Status}" />
                        </DataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    
    
    
    public class Log
        {
            public string Title { get; set; }
            public List<MoreDetails> MoreDetails { get; set; }
    
            public Log()
            {
                MoreDetails = new List<MoreDetails>();
            }
        }
    
        public class MoreDetails
        {
            public string Status { get; set; }
        }
    
    public class YourVM
    {
         public YourVM() // in my case i've just run it fast in code behind 
                {
                    LogList = new List<Log>
                        {
                            new Log{Title = "Hichem", MoreDetails = new List<MoreDetails>{ new MoreDetails{Status = "OK"}}},
                            new Log{Title = "Hichem"},
                            new Log{Title = "Hichem"},
                            new Log{Title = "Hichem"},
                        };
    
                }
    
    
                public List<Log> LogList { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view model that currently looks like below. The TCm type is
I would like to keep model binding when doing ajax calls from my view.
I have a ViewModel I am binding to a view: ProductViewModel model = Mapper.Map<Product,
I am new to xaml, i have following code below, my question is how
Being a new to WPF/XAML/MVVM, I've got a question. In my View, I have
I have the following XAML code: <ListBox ItemsSource={Binding Languages}> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <Image Source={Binding
I have this XAML: <ListBox x:Name=MyItemsList ItemsSource={Binding MyItems} SelectionChanged=ItemsList_SelectionChanged> The code behind assigns the
I have this code in MainPage.xaml: <controls:PivotItem Header=first> <ListBox x:Name=MyListBox Margin=0,0,-12,0 ItemsSource={Binding ListBoxItem}> <ListBox.ItemTemplate>
I have a button, and I have a boolean property in a view-model called
I'm binding a model to my Controller and I'd like to observe any changes

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.