1

I have the following XAML code:

<TextBlock x:Name="Coordinates" Text="{Binding Coors}"/>

How can I access this object from the codebehind? I'm trying to replace the hardcoded coordinates in Locaton, with the content of "Text":

private void DestinationButton_Click(object sender, RoutedEventArgs e)
{
    Location loc= new Location (20.2127297,-87.4625591);
    myMap.Center = loc;
    myMap.ZoomLevel = 8;
}
MVZ
  • 2,220
  • 5
  • 27
  • 57
  • Just use the variable "Coors". Since your text value is bound to that variable it will hold the same value as your control. – Lee O. Jul 13 '12 at 20:32
  • I agree with Lee. Just make sure you are using INotifyPropertyChanged on the TextBlock so the property is always up to date (from whatever is updating it). – Xcalibur37 Jul 13 '12 at 21:04
  • What if I'm not using any binding? what if it's just Text="Some text" ?? – MVZ Jul 13 '12 at 21:22

1 Answers1

3

Because you've provided it with an x:Name, you should be abled to access it as "Coordinates".

    var text = Coordinates.Text;
    var parsedLocation = <parsing logic here - use a Regex>
    myMap.Center = parsedLocation;

Also interesting - look at the difference between Name and X:Name here.

But Lee is right in the comments above. You should use the "Coors" property to at the value since it's databound there already.

Community
  • 1
  • 1
Ani
  • 10,826
  • 3
  • 27
  • 46
  • How would I use it in the context of my button? I want to use the content of Coordinates.text instead of "20.2127297,-87.4625591" private void DestinationButton_Click(object sender, RoutedEventArgs e) { Location loc= new Location (20.2127297,-87.4625591); myMap.Center = loc; myMap.ZoomLevel = 8; } – MVZ Jul 13 '12 at 21:17
  • Unfortunately, it says "Coordinates does not exist in the current context"... Do I need to add anything to "DestinationButton_Click(object sender, RoutedEventArgs e)" ? – MVZ Jul 13 '12 at 23:47
  • Nope. As long as "Coordinates" and the event handler are part of the same class you should be able to see it. – Ani Jul 16 '12 at 13:33