I could be going about this the wrong way, but I am not sure. I have a search bar that a user can enter an address. Then the request is sent to Google's API and set of addresses corresponding to the address entered will be displayed in my RadGridView. I have the address showing fine.
Now I want to take the lat and lng values of the addresses and find them on my map. The problem I am having is that I am unable to access the row values in my RadGridView upon selecting the address. I created a method that is hit when a user selects an address, but the grid.SelectedItem is returning null.
I could be going about this the wrong way. Any Ideas?
xaml.cs
private void SearchTB_KeyDown(object sender, KeyEventArgs e)
{
searchclose.Opacity = 1;
if (e.Key == Key.Enter)
{
var address = SearchTB.Text;
var requestUri = string.Format("http://localhost/media/TextToSpeech/TextToSpeechService.svc/getDataProxy?url=https://maps.googleapis.com/maps/api/geocode/json?address={0}&key=AIzaSyCgsNpuUoH7m6U7lqeZjlLZ3MgM15PW15o", Uri.EscapeDataString(address));
HttpWebRequest request = WebRequest.Create(requestUri) as HttpWebRequest;
request.Method = "GET";
IAsyncResult result = request.BeginGetResponse(new AsyncCallback(RequestCompleted), request);
}
}
private void RequestCompleted(IAsyncResult result)
{
var request = (HttpWebRequest)result.AsyncState;
var response = (HttpWebResponse)request.EndGetResponse(result);
Stream stream = response.GetResponseStream();
try
{
StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();
Regex rgx = new Regex("<.*\\>");
string newResult = rgx.Replace(text, "");
JObject json = JObject.Parse(newResult);
JArray results = (JArray)json["results"];
List<object> locationList = new List<object>();
ObservableCollection<object> AddressList = new ObservableCollection<object>();
if (results.Count == 0)
{
Dispatcher.BeginInvoke(new Action(() => MessageBox.Show("No results found")));
}
else
{
foreach (JObject obj in results)
{
if (obj == null)
{
Dispatcher.BeginInvoke(new Action(() => MessageBox.Show("Address returned no results")));
}
string formattedAddress = (string)obj["formatted_address"];
AddressList.Add(formattedAddress);
object loc = (object)obj["geometry"]["location"];
locationList.Add(loc);
//TODO Add exception handling
}
Dispatcher.BeginInvoke(new Action(() => addressGrid.ItemsSource = AddressList));
}
}
catch (Exception ex)
{
Dispatcher.BeginInvoke(new Action(() => MessageBox.Show("Error" + ex.Message)));
}
}
private void SelectedAddress_Click(object sender, RoutedEventArgs e)
{
object dodo = "";
dodo = addressGrid.SelectedItem;
}
xaml
<!--Address Results-->
<telerik:RadGridView Grid.Row="2" AutoGenerateColumns="False" x:Name="addressGrid" Margin="18 0 0 18" CanUserFreezeColumns="False" CanUserSelect="True"
EnableColumnVirtualization="True" EnableRowVirtualization="True" ShowGroupPanel="False" ScrollViewer.VerticalScrollBarVisibility="Visible"
CanUserDeleteRows="False" RowIndicatorVisibility="Collapsed" DataLoadMode="Asynchronous" HeaderRowStyle="{StaticResource ZoneActivityHeaderRowStyle}"
GroupPanelStyle="{StaticResource GroupPanelStyle}" Background="#00000000" VerticalGridLinesBrush="#00000000" HorizontalGridLinesBrush="#00000000" AlternationCount="2"
RowHeight="32" ShowColumnHeaders="False" RowStyle="{StaticResource ActivityRowStyle}" AlternateRowStyle="{StaticResource ActivityAlternateRowStyle}">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="Targetting" Width="35" IsReadOnly="True" IsFilterable="False" ShowDistinctFilters="True" IsGroupable="False" IsVisible="True">
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<ctrl:TargetButton x:Name="targetButton" VerticalAlignment="Center" HorizontalAlignment="Center" Visibility="Visible" />
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
<telerik:GridViewDataColumn Header="Address" Width="*" IsReadOnly="True"
IsFilterable="False" ShowDistinctFilters="True" IsGroupable="False">
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding}" Foreground="White" FontFamily="Segoe UI"
FontSize="12" VerticalAlignment="Center" Margin="5" TextWrapping="Wrap" />
<Border Background="#00000000" Height="40" Margin="-5 0" Cursor="Hand" MouseLeftButtonDown="SelectedAddress_Click" />
</Grid>
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
</telerik:RadGridView.Columns>
</telerik:RadGridView>
You could cast the sender argument of the event handler to a Border and then access the current data object using its DataContext property:
private void SelectedAddress_Click(object sender, RoutedEventArgs e)
{
Border border = sender as Border;
object dodo = border.DataContext;
string address = dodo as string;
if (!string.IsNullOrEmpty(address))
{
//...
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.