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

  • Home
  • SEARCH
  • 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 841709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:50:35+00:00 2026-05-15T05:50:35+00:00

In my list view control (or any other WPF control that will fit the

  • 0

In my list view control (or any other WPF control that will fit the situation), I would like to have one TextBlock that stays consistent for all items while another TextBlock that changes based on the value in the ObservableCollection. Here is how my code is currently laid out:

XAML

       <ListView ItemsSource="{Binding Path=MyItems, Mode=TwoWay}">      
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock x:Name="StrVal" Text="{Binding StrVal}" />         
                        <TextBlock x:Name="ConstVal" Text="{Binding MyVM.ConstVal, Mode=TwoWay}" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

Model

 public class MyItem
    {
        public string StrVal { get; set; }        
    }

ViewModel

public MyVM()
{

    ObservableCollection<MyItem> myItems = new ObservableCollection<MyItem>();
    for (int i = 0 ; i < 10; i++)            
        myItems.Add(new MyItem { StrVal = i.ToString()});

    MyItems = myItems;
    ConstVal = "1";
}

private string _constVal;
public string ConstVal
{
    get
    {
        return _constVal;
    }
    set
    {
        _constVal = value;
        OnPropertyChanged("ConstVal");
    }
}
public ObservableCollection<MyItem> MyItems { get; set; }

public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string item)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(item));
    }
}

}

Code Behind

this.DataContext = new MyVM();

The StrVal property repeats correctly in the ListView, but the ConstVal TextBlock does not show the ConstVal that is contained in the VM. I would guess that this is because the ItemsSource of the ListView is MyItems and I can’t reference other variables outside of what is contained in the MyItems.

My question is: How do I get ConstVal to show the value in the ViewModel for all listviewitems that will be controlled by the Observable Collection of MyItems.

  • 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-15T05:50:36+00:00Added an answer on May 15, 2026 at 5:50 am

    There are 2 ways I can see to do this.

    1) you can name the usercontrol that you are within (x:Name=control or whatever) and then do.

    <TextBlock x:Name="ConstVal" Text="{Binding ElementName=control, Path= DataContext.ConstVal, Mode=TwoWay}" />
    

    2) use relative source:

        <TextBlock x:Name="ConstVal" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Path=DataContext.ConstVal, Mode=TwoWay}" />
    

    EDIT:

    3) Because of the way that you are nesting your items (i.e. with no new datacontexts being introduced you can simply do:

    <TextBlock x:Name="ConstVal" Text="{Binding Path=DataContext.ConstVal, Mode=TwoWay}" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list view control which at the moment only allows one item
I want a list view control (or any list like control) in which each
We have used Custom List View inside that one Text View and two Edit
Hello i have a list view control, While the form is being loaded i
I have a drop-down list hardcoded in an MVC View User Control page and
How to implement CEditListCtrl?. List control with edit capabality (Report/Grid view). I have a
I have a simple requirement as mentioned below: A ListView or any control displays
I use the MFC list control in report view with grid lines to display
One can get the text of the selected item in the list-view of a
I am designing a WPF user control which contains other user controls (imagine a

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.