Reputation:
I added a class file to my project. I want to be able to access all the form controls in the code behind (grid, textboxes, labels, etc...) but I don't have access to them like I do in the main. Do I need to reference the main in the added class? This is a WPF project!
I left out code to keep this post small:
namespace ClockMVC
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ClockViewModel model = new ClockViewModel();
public MainWindow()
{
this.InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
here is the class
namespace ClockMVC
{
class ClockViewModel : INotifyPropertyChanged
{
// ClockViewModel model = new ClockViewModel();
private readonly System.Timers.Timer _timer;
public ClockViewModel()
{
Upvotes: 0
Views: 109
Reputation: 30097
If you want to know how to bind line's visibility as you say in your comment try something like this..
//in your view model.. create a visibility property against your line
Public Visibility Line1
{
get
{
return m_Line1Visibility;
}
set
{
if(value != m_Line1Visibility)
{
m_Line1Visibility = value
OnPropertyChanged("Line1");
}
}
}
in your view do something like this
<Line Visibility = {binding path = Line1}/>
Now whenever you want to show or hide Line1 change the property, if bindings are setup properly, everything will magically work.
For example Line1 = Visibility.Hidden
to hide and Line1 = Visibility.Visible
to show
Upvotes: 0
Reputation: 184516
You would need to have a reference to an instance of the MainWindow in that class, additionally you should expose the controls as properties since by default they are internal fields.
Normally you do not directly use controls via reference anyway since most stuff is done via bindings, or you can get the controls from respective event handlers (cast sender
) and pass it to a method in the ViewModel which has the respective parameter. Since most data is bound you rather modify the data and the View updates on its own.
Upvotes: 1