-1

I have an issue that I cannot solve myself and need your support. In my WPF-Application I have a MainWindow with some kind of navigation bar (based on System.Windows.Controls.Ribbon) and a content area. When I click one button1 (RibbonButton), I assign a Page to the content area of the MainWindow. In the Page I have a combobox with several values/items and I want the value to be selected that matches to the label or name of the button I pressed in the navigation. When I press the other button in the navigation, another item should be selected in the combobox.

Unfortunately, in my button_click event that is in the code behind of the MainWindow, I am not able to access the combobox of the Page-object.

Can someone help me how to access the combobox of the Page-object within the click event of the MainWindow?

Thank you and regards TPS

TPS
  • 41
  • 8
  • 1
    Use MVVM; on navigation set a variable in the model that the new view model can pick up. Doing this with code behind is going to be a *nightmare*. – BradleyDotNET Jun 18 '18 at 21:01
  • Could you add a bit more details to this (e.g. example code etc.). I know that this sounds a bit stupid, but I'm not really a developer and just know some abstract details about MVVM, Bindings in WPF etc. . Unfortunately your proposed solution is beyond my current knowledge / imagination of how to use MVVM for this issue. – TPS Jun 18 '18 at 21:04
  • Unfortunately the full code for this is way too big for an SO answer (or I would have written one). I can't recommend getting your MVVM fundamentals solid enough though. – BradleyDotNET Jun 18 '18 at 21:17
  • Ok, I‘ll try to get my MVVM fundamentals solid enough and as soon as I got it working, I‘ll answer my own question. Maybe this will help others then. BTW: I am a bit disappointed that someone downvoted my question. :-/ – TPS Jun 18 '18 at 21:29
  • 1
    I'd recommend just deleting this one and asking a much more focused one. The current solution is just way too big (will span at least four files for starters). Reading this may help you: [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236). In your case; you also have an XY problem :) – BradleyDotNET Jun 18 '18 at 21:30
  • Thank you for your response. Your first comment will serve me as a rough guideline what to focus on during my next deeper dives into MVVM. – TPS Jun 18 '18 at 21:33

1 Answers1

0

are you using MVVM? As I read you are using code behind in your code right?, I guess you have a reference to your page I right? I think you can create a public method on the Page I, and send the value you want to be set in the commbobox through that method, then if you are using mvvm, send that value to your viewmodel property asociated with the combobox SelectedItem.

private void ribonButton_click()
{ thePageI.SetComboboxwith("this value");
 .....
}

in the PageI

public void SetCombobox( string theValue){
 myViewModel.SetSelectedItem(theValue);
}

Something like that

KillemAll
  • 633
  • 1
  • 10
  • 25
  • When I asked the question I was using code behind for the navigation. Based on BradleyDotNET's comment, I switched to MVVM - meaning: *Views*: I have one MainWindow (incl. the navigation incl. i.a. two 'navigation'-buttons) with a ContentControl and one Content View (a UserControl with i.a. the combobox). *ViewModels*: One ViewModel for each of the Views. *Model*: A class with two variables (A List of this variables is displayed in the combobox). What I got working so far, is that I bound one navigation-button via an ICommand in the ViewModel of the MainWindow to the contentcontrol. – TPS Jun 20 '18 at 09:15