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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:34:39+00:00 2026-06-01T22:34:39+00:00

I’m very new to WPF and am writing an application using this example as

  • 0

I’m very new to WPF and am writing an application using this example as a starting point
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090025

I will only have one workspace visible at any one time, so I want to get rid of the TabControl and use something simple instead – probably a ContentControl, I’m really not sure but all it needs to do is have content in and be closable. So I am trying to replace this:

<DataTemplate x:Key="WorkspacesTemplate"><TabControl 
  IsSynchronizedWithCurrentItem="True" 
  ItemsSource="{Binding}" 
  ItemTemplate="{StaticResource ClosableTabItemTemplate}"
  Margin="4"
  />

with:

<DataTemplate x:Key="WorkspacesTemplate">
    <ContentControl Content="{Binding ??}" ContentTemplate="{StaticResource ClosableTabItemTemplate}"/>
</DataTemplate>

but I don’t know what to bind to. The code in the example seems to use CollectionViewSource to set the active workspace – it’s the active workspace that I am interested in but I don’t understand what TabControl is doing except that it’s something to do with IsSynchronizedWithCurrentItem=”True”

The template is invoked from here (Workspaces is the ObservableCollection of ViewModels):

<HeaderedContentControl Content="{Binding Path=Workspaces}" ContentTemplate="{StaticResource   WorkspacesTemplate}" Header="Workspaces" Style="{StaticResource MainHCCStyle}"/>

and here is the ClosableItem template:

  <DataTemplate x:Key="ClosableTabItemTemplate">
    <DockPanel Width="120">
        <Button 
    Command="{Binding Path=CloseCommand}"
    Content="X"
    Cursor="Hand"
    DockPanel.Dock="Right"
    Focusable="False"
    FontFamily="Courier" 
    FontSize="9"
    FontWeight="Bold"  
    Margin="0,1,0,0"
    Padding="0"
    VerticalContentAlignment="Bottom"
    Width="16" Height="16" 
    />
        <ContentPresenter 
    Content="{Binding Path=DisplayName}" 
    VerticalAlignment="Center" 
    />
    </DockPanel>
</DataTemplate>

Please can someone explain what I need to do? Thanks

  • 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-01T22:34:41+00:00Added an answer on June 1, 2026 at 10:34 pm

    The WorkspacesTemplate is telling WPF how to display the Workspaces property, which, as you say is an ObservableCollection of ViewModels.

    So, the WorkspacesTemplate says, display all these ViewModels in a tab control, and for each ViewModel, use the ClosableTabItemTemplate to display the ViewModel in a tab.

    Since you only want one workspace visible at a time, you don’t need to expose a collection of workspaces from your ViewModel, and you don’t need a tab control to display them. You simply expose the one current workspace from your ViewModel and provide some XAML to display it.

    If you still want to use a template to wrap the ViewModel, then yes, you can just use a ContentControl to invoke the template:

    <DataTemplate x:Key="MySingleWorkspaceTemplate">
        <TextBlock Text={Binding Blah} />
        <!-- etc -->
    </DataTemplate>
    

    and to invoke the template

    <ContentControl Content="{Binding CurrentWorkspace}" ContentTemplate="{StaticResource MySingleWorkspaceTemplate}"/>
    

    However, if this is the only place that the XAML is going to be used, you might as well forget the template and just declare the XAML directly. Eg, (instead of ContentControl)

    <TextBlock Text={Binding CurrentWorkspace.Blah} />
    <!-- etc -->
    

    EDITED TO ADD:

    I think you might be getting confused because currently the ViewModel has no concept of the “Selected Workspace”, it just exposes a collection. For completeness (but don’t worry about all this), the selection is introduced by the TabControl which indirectly uses the default CollectionView for the Workspaces collection, and CollectionView has the concept of a selected item. This is all in the view.

    I wouldn’t worry about any of this now, just expose the one workspace yourself from your ViewModel.

    EDIT2:

    Your close button is appearing because you are explicitly setting a ContentTemplate on your HeaderedContentControl. This template will appear regardless of Content.

    To make a template only appear when there is data in Content, make the template implicit instead. If you add a DataType to your template definition (and remove the key), you tell WPF to always use this template to display an object of that data type.

    <DataTemplate DataType="{x:Type vm:WorkspaceViewModel}">
        <!-- Blah -->
    </DataTemplate>
    

    Then you can remove the explicit template from your HeaderedContentControl. Simply setting the Content will be enough to invoke the template, and if there is no Content, there is no template.

    <HeaderedContentControl Content="{Binding Path=CurrentWorkspace}" />
    

    (ps. If you’re not using the header of HeaderedContentControl, you might as well just use a bog standard ContentControl)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.