Reputation: 49
I am feeling really stupid right now. I have a page with a listing of transactions. When a user selects an item on the grid, I load the properties for that selection into a details page. I am binding and properly displaying the details in XAML, But I need to access the property values in code behind to make some subsequence calls to an API. I am drawing a blank on how to achieve that here.
<Label Grid.Column="3"
Grid.Row="2"
Text="{Binding Amount}" />
<Label Grid.Column="4"
Grid.Row="2"
Text="{Binding Status}"
VerticalOptions="End" />
So how would I get the value of Status and Amount in Code Behind.
Upvotes: 0
Views: 2173
Reputation: 12721
Here is a sample to explain how to get content from Control.
VeggieViewModel class :
public class VeggieViewModel
{
public string Name { get; set; }
public string Type { get; set; }
public string Image { get; set; }
}
Create a ViewModel with specified data :
public class ViewModel
{
public ObservableCollection<VeggieViewModel> veggies { get; set; }
public ViewModel()
{
veggies = new ObservableCollection<VeggieViewModel>();
veggies.Add(new VeggieViewModel { Name = "Tomato", Type = "Fruit", Image = "tomato.png" });
veggies.Add(new VeggieViewModel { Name = "Romaine Lettuce", Type = "Vegetable", Image = "lettuce.png" });
veggies.Add(new VeggieViewModel { Name = "Zucchini", Type = "Vegetable", Image = "zucchini.png" });
}
}
Xaml code as follow :
<ListView x:Name="lstView" RowHeight="60" ItemSelected="lstView_ItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
</StackLayout>-->
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Grid.RowSpan="2" Grid.Column="0" Source="{Binding Image}"/>
<Label Grid.Row="0" Grid.Column="1" Text="{Binding Name}" FontSize="24" />
<Label Grid.Row="1" Grid.Column="1" Text="{Binding Type}" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Now in ContentPage , binding data for ListView and deal with ItemSelected
method .I will get content of Name
and Type
,that's the same
with your want of Status
and Amount
.
public MainPage ()
{
InitializeComponent();
ViewModel viewModel = new ViewModel();
lstView.ItemsSource = viewModel.veggies;
}
private void lstView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var obj = (VeggieViewModel)e.SelectedItem;
Console.WriteLine("-name is-" + obj.Name + "-type is-" + obj.Type);
DisplayAlert("Show Message", "-name is-" + obj.Name + "-type is-" + obj.Type, "OK");
// other code
}
The effect :
Upvotes: 1
Reputation: 21
You can add name for label and then get property in code behind.
<Label x:Name="label" Grid.Column="4"
Grid.Row="2"
Text="{Binding Status}"
VerticalOptions="End" />
var value = label.Text // get value in code behind
Also you can get value from BindingContext. You need cast you binding context in you view model type
if(BindingContext is YouViewModelType context)
{
var value = context.Status
}
Upvotes: 1