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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:16:35+00:00 2026-05-30T02:16:35+00:00

In my application I’d like to have a drop-down box to choose a table

  • 0

In my application I’d like to have a drop-down box to choose a table to edit (of about 20). Each table should be represented by its own WPF DataGrid. (I’d thought about using a single DataGrid and creating a set of new columns at runtime with code-behind, but that doesn’t seem very XAML-ish.)

My drop-down is in a UserControl (since it’s part of a larger application). I believe (from my research) that the placeholder for the one-of-20 DataGrids should be a ContentControl used as a placeholder here:

<UserControl x:Class="MyClass" ...
         xmlns:my="clr-namespace:MyNamespace"
         DataContext="{Binding ViewModel}">
<StackPanel>
    <Grid>
        <ComboBox Name="DataPaneComboBox" HorizontalAlignment="Stretch" 
                  IsReadOnly="True" MinWidth="120" 
                  Focusable="False" SelectedIndex="0"
                  DockPanel.Dock="Left" Grid.Column="0"
                  SelectionChanged="DataPaneComboBox_SelectionChanged">
            <ComboBoxItem Name="FirstOption" Content="Choice 1" />
            <ComboBoxItem Name="SecondOption" Content="Choice 2" />
            <ComboBoxItem Name="ThirdOption" Content="Choice 3" />
        </ComboBox>
    </Grid>
    <ContentControl Name="DataGridView" Margin="0,3,0,3" Content="{Binding CurrentView}" />
</StackPanel>

Here’s my code-behind for this class:

public partial class MyClass : UserControl {
    private MyViewModel ViewModel {
        get; set;
    }

    public MyClass() {
        InitializeComponent();
        ViewModel = new MyViewModel();
        ViewModel.CurrentView = new DataGridChoice1();
    }
}

And the ViewModel (class ObservableObject implements the INotifyPropertyChanged interface):

public class MyViewModel : ObservableObject {
    private UserControl _currentView;

    public UserControl CurrentView {
        get {
            if (this._currentView == null) {
                this._currentView = new DatGridChoice1();
            }

            return this._currentView;
        }
        set {
            this._currentView = value;
            RaisePropertyChanged("CurrentView");
        }
    }
    #endregion
}

And one of the 20 or so UserControls that can be substituted in at runtime:

<UserControl x:Class="Choice1Control"
             xmlns:my="clr-namespace:MyNamespace">
    <DataGrid ItemsSource="{Binding Choice1Objects}" />
        <!-- ... -->
    </DataGrid>
</UserControl>

When the user changes the drop-down I’d like the program to load the appropriate DataGrid. Right now I can’t see the child UserControl (here, Choice1Control). I’ve added the child directly (without the intervening ContentControl) and it works fine.

I’ve tried just about every combination of DataContext and UserControl content binding. I’m new to WPF, so I’m probably missing something glaringly obvious. 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-05-30T02:16:36+00:00Added an answer on May 30, 2026 at 2:16 am

    For some reason it never occurred to me that there would be Binding errors written clearly in the log at run time. I futzed around until I actually got a useful message and could get to the root of the problem.

    It seems that the DataContext of the root UserControl was getting intercepted before it could be inherited by the ContentControl. (Or, my impression of how DataContext is inherited/propagated is wrong.)

    In the end I changed the MyClass constructor to explicitly specify the DataContext as the ViewModel.

    public MyClass() {
        InitializeComponent();
        ViewModel = new MyViewModel();
        ViewModel.CurrentView = new DataGridChoice1();            
        this.DataContext = ViewModel;                      // <-- Added this line
    }
    

    Bindings then work as expected, and I’m able to change between multiple DataGrids as the drop-down box changes state.

    I’d love to hear why the initial binding was wrong. However, I’ll claim a small victory now and leave that conundrum for another day.

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

Sidebar

Related Questions

Application[temp] = 8; should set the value 8 to the key temp . However,
The application I'm currently writing is using MVVM with the ViewModel-first pattern. I have
Application uses Entity Framework 4.1 with database first approach. I have in database a
Application shows information about planets, their moons, and etc. It shows a list of
Application is a C# .Net 3.5 WCF Service. I'd like during the build process
Application use NHibernate. I Have object A that contains set of objects B. I
My application is suspending on a line of code that appears to have nothing
Application has 7-8 activities, so I have create an application with some background music
My application scenario is like this: I want to evaluate the performance gain one
Application able to record error in OnError, but we are not able to do

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.