0

I have a RootViewModel class, and I want to access an UI element (instantialized in MainWindow) from there. For that I set the class this way:

 class RootViewModel : MainWindow, INotifyPropertyChanged

But the application doesn't start. It compiles and throws no error but the Window doesn't appear. If I remove that MainWindow, I can't access my element that has been created in MainWindow.xaml. What can I do to solve this?

EDIT: Ok, I understand that I shouldn't be doing that, it's going against what it is MVVM. But is there a way to modify directly something from MainWindow? What should I try instead of this?

Sturm
  • 3,968
  • 10
  • 48
  • 78
  • 5
    What are you trying to achieve? This seems on the surface, like a horrible misinterpretation of MVVM. – Gusdor Jun 05 '13 at 07:46
  • A ViewModel shouldn't inherit from a UI related class... it doesn't make any sense – Thomas Levesque Jun 05 '13 at 07:48
  • Maybe it is, I was trying to bind the DynamicDataDisplay plotter to some data, that is loaded with a dynamically created loader. But it's hard to figure out how to bind "LineGraphs" (the kind of objects that holds the plotter) to it. So my (certainly wrong) approach is to try to set it manually. – Sturm Jun 05 '13 at 07:50
  • Did you replace the `MainWindow.xaml.cs` file, or just extend it in another file? – Tieson T. Jun 05 '13 at 07:53
  • @Tieson T. what do you mean by extending it in another file? I don't quiet understand that. Thanks – Sturm Jun 05 '13 at 07:59
  • Do you have a `RootViewModel.cs` file? – Tieson T. Jun 05 '13 at 08:05

3 Answers3

0

Consider changing RootViewModel to a UserControl. Give it a DependencyProperty called Element, of type UIElement.

Add RootViewModel to the XAML for MainWindow and bind to the element you want to use, like this;

<RootViewModel Element="{Binding ElementName=SourceElement}"/>
Gusdor
  • 14,001
  • 2
  • 52
  • 64
0

WPF windows are objects, so you can always instantiate them manually, like so:

var foo = new FooWindow(); // new Window object
foo.Show(); // show window as non-blocking "dialog"

If you do that, you have access to any public or protected members of the window - that includes any child controls, as long as their Accessibility properties are marked accordingly. So, if FooWindow had a TextBox named txtFooName, you could access it like so:

string name = foo.txtFooName.Text // get string value from textbox

You can also assign to any public/protected members:

foo.txtFooName.Text = "Fizz Buzz, Inc.";

Now, MainWindow is generally set as the StartupUri of the application (in App.xaml), which makes it the entry point for the application, so I'm not entirely sure what you're trying to do.

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
0

I was able to achieve what I wanted by creating a

public ObservableCollection<ChartPlotter> myPlotCollection { get; set; }

And then adding a ChartPlotter there, and setting in XAML:

 <DockPanel Grid.Column="2">
        <ItemsControl  Width="Auto" 
                   Height="Auto"
                   ItemsSource="{Binding myPlotCollection}">
        </ItemsControl>

    </DockPanel>

So this way I have complete control over what is happening in myPlotCollection[0]. At this moment it's enough for me, later I'll give it another try to bind it properly.

Sturm
  • 3,968
  • 10
  • 48
  • 78
  • don't forget to mark your answer as answer. you could also delete your question if you think it won't help someone in the future – WiiMaxx Jun 05 '13 at 08:48