I saw this piece of code in an example of some Xamarin code:
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding .}" TextColor="#66ffff" FontSize="Micro" HorizontalOptions="CenterAndExpand" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
What does Binding . mean? My guess from trying out the code is one character from the ItemsSource like in a regular expression? But I can't find this in the documentation.
The point (.) means that you bind the whole object/ model instead of a single property to the text property of that label. This is sometimes required, for example you need multiple aspects from your model in a converter.
In this code example above, it also could be that the item source for that listview is a list of strings. So there is no property to bind to the text property.
Related
<Picker
Title="Select a Profile"
ItemsSource="{Binding ProfileManager.Profiles}"
ItemDisplayBinding="{Binding Name}"
SelectedItem="{Binding SelectedProfile, Mode=TwoWay}"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
SelectedIndex="1"
HeightRequest="200"
WidthRequest="325"
Margin="10"
BackgroundColor="{StaticResource Secondary}">
</Picker>
the itemSource property have x:DataType="ViewModels:ParametersViewModel" datatype but the ItemDisplayBinding have x:DataType="Models:Profile" datatype.
How can I define these datatypes seperatly?
Not sure If this answer works for you but you can define different data types inside same XAML file by clarifying Ancestor Type.
<ContentPage
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:Models="clr-namespace:Models"
xmlns:ViewModels="clr-namespace:ViewModels"
x:DataType="viewmodel:ParametersViewModel">
<Picker ItemsSource="{Binding ProfileManager.Profiles}"
ItemDisplayBinding="{Binding Source={RelativeSource AncestorType={x:Type Models:Profile}}, Path=Name}"/>
</ContentPage>
Once you set a DataType in the root (in this case ContentPage), all the bindings of its children will go to that datatype by default.
From document Populate a Picker with data using data binding, we could find that
A Picker can be also populated with data by using data binding to bind
its ItemsSource property to an IList collection.
<Picker Title="Select a monkey"
ItemsSource="{Binding Monkeys}"
ItemDisplayBinding="{Binding Name}" />
When binding to a list of objects, the Picker must be told which
property to display from each object. This is achieved by setting the
ItemDisplayBinding property to the required property from each object.
In the code examples above, the Picker is set to display each
Monkey.Name property value.
So, the ItemDisplayBinding property(Name) in your code above should be the required property from each object(Profile) in your ItemsSource(ProfileManager.Profiles).
In fact, we don't have to or need to specify what you call x:DataType for the Picker.
Note:
You can find the official sample here MonkeyAppPicker,though it is coded in xamarin , but the implementation on MAUI is almost the same.
I feel like I'm missing something obvious because this is so simple. Thanks in advance for the help. I'm struggling with binding the SelectedItem of a simple ListView in a Xamarin application. I'm testing on UWP and am using Prism's MVVM BindableBase base class. Here's what I'm experiencing:
The page loads and nothing in the list is selected.
I select an item in the list.
The setter of SelectedGrade is called with a value of null.
After that, selecting items does not cause the SelectedGrade setter to be called.
Here's the relevant XAML:
<ListView BackgroundColor="#7F7F7F"
CachingStrategy="RecycleElement"
IsPullToRefreshEnabled="True"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
ItemsSource="{Binding Grades, Mode=OneWay}"
SelectedItem="{Binding SelectedGrade, Mode=TwoWay}"
RefreshCommand="{Binding RefreshCommand}"
RowHeight="50">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout HorizontalOptions="FillAndExpand"
VerticalOptions="CenterAndExpand"
Orientation="Horizontal"
Padding="10">
<Label HorizontalOptions="FillAndExpand"
Text="{Binding Title}"
TextColor="#272832"/>
<Label HorizontalOptions="FillAndExpand"
Text="{Binding Score}"
TextColor="Aquamarine" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Here's the ViewModel:
Grade _selectedGrade;
public Grade SelectedGrade
{
get { return _selectedGrade; }
set { SetProperty(ref _selectedGrade, value); } // this gets set to NULL the first time i select an item then never gets set again
}
Edit: I've also added an ItemSelected event listener in the code-behind and it is not being fired after the initial item is selected. The one time it is fired, the SelectedItemChangedEventArgs reveal that the ListView's SelectedItem is null. I can also see at this time that the ListView's ItemsSource has a collection of three items in it, as I would expect. I'm quite confused as to why the ListView thinks the SelectedItem is null and why it is not broadcasting when the selected item changes.
It looks like this was a bug in Xamarin. I upgraded from Xamarin.Forms 3.0.0.550146 to 3.1.0.637273 and now it works.
I have faced with the similar issue when List does not bind to view model with SelectedItem and Xamarin forms 5.0.0.2244.
It was resolved with setting default value of List property to new List() object instead null value in the view model before setting binding context . After that binding has became working.
I am trying to create a listview where users can select the list item to view the store and also be able to click on the image to take them to the product detail.
How can I add a command in the ViewCell?
<ListView ItemsSource="{Binding myData}" x:Name="myListView" SelectedItem="{Binding SelectedStore}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Label Text="{Binding Title}" />
<Image Source="product.png" >
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ItemTapCommand}" CommandParameter="Id" />
</Image.GestureRecognizers>
</Image>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
public ICommand ItemTapCommand => new Command(ShowSelectedProductAsync());
private async Task ShowSelectedProductAsync()
{
//take them to view the item detail
}
The problem is that you are attempting to bind to a command that is not located on the individual item within the ListView; the BindingContext on your Image is the individual items inside your myData collection.
With other frameworks, for example, Wpf, you would simply use the RelativeSource part of binding to tell the control to look for your command on the BindingContext of another control, however this isn't possible in Xamarin Forms (RelativeSource is not supported).
What you can do is the following, but it requires your UserControl to have an x:Name:
<TapGestureRecognizer Command="{Bindind Path=BindingContext.ItemTapCommand,
Source={x:Reference MyUserControl}}"
CommandParameter="{Binding Id}" />
What this says is to use the UserControl as the source for the binding, and the property is a nested property on that UserControl composed of BindingContext.ItemTapCommand.
As a bonus, I updated the code because I felt that you probably meant to bind the value of the Id property of each record as the command parameter, and not merely to send the string "Id" as the command parameter.
On my MainWindow.xaml page, I have the following code which works (where MyWord is a string)
<ContentControl Content="{Binding MyWord}" />
I'm playing with DataTemplates and trying understand them. So, I want to reference a DataTemplate from within my ContentControl. The DataTemplate should contain a TextBlock which binds to my string. I updated my code
<ContentControl ContentTemplate="{StaticResource ViewsTemplate}" />
And in my ResourceDictionary I add
<DataTemplate x:Key="ViewsTemplate">
<TextBlock Text="{Binding MyWord}" />
</DataTemplate>
This produces no text on screen at all. I even tried
<ContentControl Content="{Binding MyWord}" ContentTemplate="{StaticResource ViewsTemplate}" />
and still no result on screen.
I can't work out why can any one give some advice please.
Thank you
The ContentControl still needs to have some content bound to it.
<ContentControl ContentTemplate="{StaticResource ViewsTemplate}" Content="{Binding MyWord}" />
Would work, but then you'd need to change your data template because it expects to be able to find MyWord which of course it won't be able to, so you'd want to use just {Binding} instead.
Alternatively, bind the ContentControl's Content to {Binding} - the current DataContext of its parent - and leave the template as it is.
I am new to the XAML world and I'm fumbling my way through a lot of tutorials. One thing i'm stuck on is calling the .tostring on an object.
Here is mysetup
I have a listbox that is bound to a list of objects
I have a contentControl bound to the same list that displays the selected item from the listbox.
My ContentControl is as follows:
<ContentControl Grid.Row="1" Margin="0,3,5,204" Name="Detail"
Content="{Binding Source={StaticResource listingDataView}}"
ContentTemplate="{StaticResource myContentTemplate}"
HorizontalAlignment="Right" Width="231"/>
in myContentTemplate I have:
<DataTemplate x:Key="myContentTemplate">
<StackPanel>
<TextBlock Text="{Binding Path=Name}" />
<!-- want to call .tostring here-->
</StackPanel>
</DataTemplate>
In the template I'd like to call .tostring on the object that is currently selected but i cant figure out how to do that?
thanks
Steph
well looks like i asked this too soon. I found the answer in another question
How to reference current object in XAML
Quoting the answer from that thread:
According to the Data Binding
Overview, you can use the "/" to
indicate the current item. You can
then navigate up and down the tree as
needs be using the following type
syntaxes:
Button Content="{Binding }" />
Button Content="{Binding Path=/}" />
Button Content="{Binding
Path=/Description}" />
Hope this helps someone else :)