0

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()
        {
Gustavo Mori
  • 8,319
  • 3
  • 38
  • 52
  • 1
    Why do you want to access MainWindow's controls in another classs which seems like a view model? – Haris Hasan May 13 '11 at 06:04
  • I know what your saying. I have a OnPropertyChanged and want to bind to turn visibility on or off on a line. can this be done? –  May 13 '11 at 06:09
  • Yes this can be done and would be more neat than this approach. I guess you need some basic MVVM tutorials to starts with. Have a look at http://stackoverflow.com/questions/1405739/mvvm-tutorial-from-start-to-finish – Haris Hasan May 13 '11 at 06:12
  • I have the view model set up and working. I can display time in a text box via on property chnage. But i want to bind to lines or draw lines to simulate 7 segment display like in a real digital clock. i am havind=g a hard time binding and dispaying a line on a canvas via on property chnage –  May 13 '11 at 06:41

2 Answers2

1

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.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • I have a OnPropertyChanged and want to bind to turn visibility on or off on a line in xaml. can this be done? –  May 13 '11 at 06:10
  • Yes, see the question haris linked to in his comment. – H.B. May 13 '11 at 06:14
0

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

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
  • that is something i want to do is have a grid with laid out lines like 7 segments and just toggle visibility between segments to time. so, for hour 12 i would split it up between 2 variables and run it threw a case and just toggle the segments to look like a one or two or three. however, i have a view model class and i can reference line1 from xaml. so, i can probably do it in the main form but then it wouldnt be a view model? –  May 13 '11 at 21:31
  • Yes, that would be just a logic in code behind not a view model – Haris Hasan May 13 '11 at 21:37
  • It wont let me make a Public Visibility Line1 - error = "already contains a definition for 'LineA'" dont i name the line and the visibility the same? –  May 15 '11 at 01:22
  • if i leave out the name in the xamle for line it just does nothing, –  May 15 '11 at 01:37
  • If you are going for code behind file approach you don't need these properties. In that case you can directly access the variable defined in XAML and set its visibility. If you have declared LineA in XAML and you are in code behind, then you wont be able to declare a property with same name. No, property and the line name should not be same. – Haris Hasan May 15 '11 at 07:46