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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:15:00+00:00 2026-05-25T18:15:00+00:00

I have a WPF app with interchangeable views. A view inherits from my ViewBase

  • 0

I have a WPF app with interchangeable views. A view inherits from my ViewBase class:

public class ViewBase : ContentControl, IView
{
    public ViewBase()
    {
        ToolbarGroups = new ObservableCollection<ToolbarGroup>();
    }

    public ObservableCollection<ToolbarGroup> ToolbarGroups
    {
        get { return (ObservableCollection<ToolbarGroup>)GetValue(ToolbarGroupProperty); }
        private set { SetValue(ToolbarGroupProperty, value); }
    }

    public static readonly DependencyProperty ToolbarGroupProperty = 
                DependencyProperty.Register("ToolbarGroup", typeof(ObservableCollection<ToolbarGroup>), typeof(ViewBase));
}

I am binding the ToolbarGroups property to an ItemsControl in my window. It works great.

A ToolBarGroup inherits from HeaderedContentControl, and I wire them up inside a view like this:

<base:ViewBase x:Class="TestApp.Orders.OrderDetailsView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:base="clr-namespace:TestApp"
               xmlns:local="clr-namespace:TestApp.Orders"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
               x:Name="root">

    <base:ViewBase.ToolbarGroups>
        <base:ToolbarGroup Header="Group1">
            <!-- Buttons and other controls go here -->
        </base:ToolbarGroup>

        <!-- Binding to MyProp is the problem -->
        <local:PremadeToolbarGroup MyProp="{Binding}" />

        <!-- This also doesn't work -->
        <local:PremadeToolbarGroup MyProp="{Binding ElementName=root,Path=DataContext}" />
    </base:ViewBase.ToolbarGroups>

        <Grid>
            <!-- this binding works -->
            <TextBox Text="{Binding Path=Model.FullName}" />
        </Grid>
</base:ViewBase>

The PremadeToolbarGroup is a XAML file that inherits from ToolbarGroup and adds some buttons. The PremadeToolbarGroup contains a dependency property called MyProp which I want to bind to the DataContext of the view its used on.

My DataContext is set to an object of type ViewModelBase, which is also the type for the MyProp dependency property.

The PremadeToolbarGroup shows up as expected, but the binding never works (MyProp is always null). Any thoughts?

  • 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-25T18:15:01+00:00Added an answer on May 25, 2026 at 6:15 pm

    I found a solution:

    On my window, the view is shown in a ContentPresenter:

    <ContentPresenter Margin="3" x:Name="ViewPresenter" />
    

    The content property is set to the View via the code-behind.

    As I said in my question, “I am binding the ToolbarGroups property to an ItemsControl in my window.”

    So I display the ToolbarGroups elsewhere in the window with this:

    <ItemsControl ItemsSource="{Binding ElementName=ViewPresenter, Path=Content.ToolbarGroups}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
    

    The ToolbarGroup objects couldn’t bind to the DataContext of the View because they were added to the visual tree outside the view.

    I was able to fix it by binding the DataContext of each item in the ItemsControl to the DataContext of the ViewPresetner content. It looks like this:

    <ItemsControl ItemsSource="{Binding ElementName=ViewPresenter, Path=Content.ToolbarGroups}">
        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="FrameworkElement.DataContext" Value="{Binding ElementName=ViewPresenter,Path=Content.DataContext }"/>
            </Style>
        </ItemsControl.ItemContainerStyle>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
    

    It’s a bit messy but it works. I’m certainly open to a more elegant solution if anyone has one.

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

Sidebar

Related Questions

I am new to WPF and .net. I have a WPF app which someone
I am new to both WPF and WCF, and have a WPF app that
I have a WPF app that updates my database from an in-code entity model.
I have a WPF app that from time to time needs to perform a
I have a WPF app that is currently driven completely from XAML. All fields,
I have a WPF app with a listbox and a contentcontrol. The contentcontrol content
I have a WPF app which snaps to screen edges (I just set the
I have a WPF App which is grinding to a halt after running out
I have a WPF App that implements a ListView. I would like to show
I have a WPF app in which I need to programatically manipulate at runtime.

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.