MilkBottle
MilkBottle

Reputation: 4332

How to pass textBlock control to a Class

say I have a textBlock control and I want to pass it to a class which controls the textBlock to display certain Message.

1) When I call a method in the class, I want textBlock to show message. Example " Checking connection...."

2) When the method complete the required task, the textBlock visibility become collapsed.

In the XAML : I have

a) textBlock name=textBlockMsg b) a Button to call the class

Appreciate your help.

-- Update :

This class file inside project


public class  GeoCalculation
{

     public GeoCalculation()     {  }


  public void  CalculateDistance()
  {
        //- Begin -- want the textBlockMsg show  :  in progress......

       --code 

     //-- when end-----, textBlockMsg visibility becom collapse


   }

}

Upvotes: 0

Views: 219

Answers (2)

Dominik Kirschenhofer
Dominik Kirschenhofer

Reputation: 1205

  1. You can have a parameter to pass the TextBock:

    public void CalculateDistance(TextBlock tb) { tb.Text = "in progress..."

    --code

    tb.Visibility = Visibility.Collapsed; }

  2. You coud use a the constructor of your class to inject the textblock it should handle

    public class GeoCalculation { private TextBlock _tb;

    public GeoCalculation(TextBlock tb)
    {
        _tb = tb;
    }
    
    public void  CalculateDistance()
    {
        _tb.Text = "in progress..."
    
       //code 
    
       _tb.Visibility = Visibility.Collapsed;
    }
    

    }

A ViewModel and using DataBinding would be better by the way! There you could use our class (method) to provice the text for the ui (textbox)

But be aware: There is a .net way to do this. The GeoCoordinate class contains a method "GetDistanceTo" to calculate the distance between two geo points. See http://msdn.microsoft.com/en-us/library/system.device.location.geocoordinate.getdistanceto.aspx .

Upvotes: 0

MBen
MBen

Reputation: 3996

If you named you TextBox in the XAML with textBlockMsg, this will work

Edit // I will not implement the whole INotifyPropertyChanged check how to do to it : implement

public class CalculationClass : INotifyPropertyChanged    
{
      public void  CalculateDistance()
      {
       TextToBeBound = "in progress..."

       --code 

       VisibilityToBeBound = Collapsed;

      }
      public string TextToBeBound
      {  //... insert the implement of this property + NotifyPropertyChanged
        get {...} 
        set {...}
      }

      public Visibility VisibilityToBeBound
      {  //... insert the implement of this property + NotifyPropertyChanged
        get {...} 
        set {...}
      }

}

Then in the XAML add this :

<TextBlock x:Name="txtBlocMsg"  Visibility={"Binding VisibilityToBeBound"}  Text={Binding TextToBeBound"}/>

Don't forget to set the DataContext of the UI to your class (in my case CalculationClass

You should be good to go. If all this was new. I recommend you read about data Binding + MVVM pattern.

Edit It's bad practice to pass UI element to model/business classes. You should use the MVVM pattern.

Hope this helps.

Upvotes: 2

Related Questions