how to specify a binding value in a collectionview?

xamarin forms collectionview example
xamarin collectionview item tapped
xamarin forms collectionview selecteditem
collectionview vs listview
xamarin forms collectionview horizontal
xamarin forms collectionview selectionchangedcommand
xamarin collectionview grouping
xamarin forms collectionview pull to refresh

I created a CollectionView inside which has a custom View.

I want to specify the property of the customview dynamically so I add a Binding Value to the customView but obviously it need a Property in the Binding Data.

But I want to specify it in the cs file.

<CollectionView ItemsSource="{Binding Items}">
<CollectionView.ItemTemplate>
<DataTemplate>
<local:MyView WidthRequest="{Binding Width}"/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

obviously I need add a Width property in the Items.But I do not want it. Is there anyway to do it?

Different from Button and Image . ContentView will not have a default size if you don't set the child elements .

So if you want to set the size in runtime , use data binding is the best way . If you don't want to define the property in model and set it multi times , you can set the binding path of the ContentView . In this way you should make sure that each row has the same height .

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:XXX"
             mc:Ignorable="d"
             x:Name="contentPage"  // set the name
             x:Class="XXX.MainPage">
<local:MyView HeightRequest="{Binding Source={x:Reference contentPage},Path=BindingContext.Height}" />
in ViewModel
public double Height {get; private set;}

Xamarin.Forms CollectionView Data, CollectionView includes the following properties that define the data to be displayed, specifies the collection of items to be displayed, and has a default value of null . For more information about data binding, see Xamarin. A little known and hence under-used aspect of WPF Binding is the ability to bind to the " current " item in a collection's view. Broadly speaking, this substitutes the "/" binding symbol for the dot. For example between collection and property name. Read more here.

Try to use <Grid> with one column specifying it's width as Auto instead of binding dynamic Width:

<CollectionView ItemsSource="{Binding Items}">
    <CollectionView.ItemTemplate>
    <DataTemplate>
        <Grid>
           <Grid.ColumnDefinitions>
              <ColumnDefinition Width="Auto"/>
           </Grid.ColumnDefinitions>
           <local:MyView Grid.Column="0"/>
        <Grid>
    </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

CollectionView Class (System.Windows.Data), You can think of a collection view as a layer on top of a binding source collection Returns a value that indicates whether the specified item belongs to the view. SelectedItem, of type object, the selected item in the list. This property has a default binding mode of TwoWay, and has a null value when no item is selected. SelectionChangedCommand, of type

I have a not so better solution. I created a style which specify the HeightReqeust and WidthRequest of the customView.

and I specify the value in the cs file.

till now I have not found a better solution.

Migrating ListView To CollectionView in Xamarin.Forms , <CollectionView SelectionMode="Single" SelectedItem="{Binding SelectedItem}" We set SelectedItem = null to remove the cell highlight, which is common to do if Value="true"> <Setter Property="Text" Value="Refreshing. You need to explicitly specify a source, i would suggest you name the control with your DataContext and use x:Reference to get it (ElementName will not work) or you use a StaticResource, e.g. <CollectionContainer Collection="{Binding DataContext.GreekGods, Source={x:Reference myStackPanel}}"/>

DataSource Binding with UI Controls, This article describes how to bind TKDataSource and customize those controls. alloc] initWithName:@"John" value:50 group:@"A"]]; [items addObject:[[DSItem alloc] Just set the dataSource property and prepare the collection view:. If you set the property IsSynchronizedWithCurrentItem to True on the view that the collection is bound to, it automatically synchronizes the current item of the CollectionView and the View. <ListBox ItemsSource = "{Binding Customers}" IsSynchronizedWithCurrentItem = "True" />

How To Work With CollectionView in Xamarin Forms, CollectionView is available on iOS and Android, but is only partially By using Binding property, we set the values of Contacts(AllContacts) to� Alternatively, this can also be accomplished by setting the ItemsLayout property to a LinearItemsLayout object, specifying the Vertical ItemsLayoutOrientation enumeration member as the Orientation property value: <CollectionView ItemsSource="{Binding Monkeys}"> <CollectionView.ItemsLayout> <LinearItemsLayout Orientation="Vertical" /> </CollectionView.ItemsLayout> </CollectionView>

Collection View header binding � Issue #8388 � xamarin/Xamarin , Summary Can add a label to the header of collection view and bind the instead specify my bindable elements as children of <CollectionView. ItemsSource=" {Binding Items}"> <CollectionView.HeaderTemplate> <DataTemplate> <Label Text=" {Binding Title}" /> </DataTemplate> </CollectionView.HeaderTemplate> </CollectionView>. You can see a full example here and here. hartez closed this on Nov 5, 2019.

Comments
  • Could you provide the full code of the xaml and your custom view?
  • please just add a ContentVIew in a CollectionView's DataTemplate and see if it displayed if you do not specify a height property
  • You can check my answer .
  • It is worked but if I use row to specify it's height with auto it is not worked.
  • can you post the screen how it look like?
  • it only has a 1 pixel height.