Home > Mobile >  .Net MAUI - How to bind to a property in an object that is a List of KeyValuePairs
.Net MAUI - How to bind to a property in an object that is a List of KeyValuePairs

Time:08-26

Completely new to .Net MAUI.

I have the following "model" (application data) class that includes a property that is a list of KeyValuePair<String, String>, as shown below:

   public class app_data
   {

      private Int32 m_num_fld;

      private List<KeyValuePair<String, String>> m_kvp_list;


      public app_data()
      {

         m_num_fld = 100
      
         m_kvp_list = new List<KeyValuePair<String, String>>()
         {

            new KeyValuePair<String, String>( "Key 1", "Value 1" ),
            new KeyValuePair<String, String>( "Key 2", "Value 2" )

         };
         
      }


      public Int32 num_fld
      {

         get => m_num_fld;

         set => m_num_fld = value;

      }


      public List<KeyValuePair<String, String>> kvp_list
      {

         get => m_post_data_kv_list;

         set => m_post_data_kv_list = value;

      }

   }

Within XAML (shown below), how does one "bind" the kvp_list within app_data to show the keys and values? If someone can post a step-by-step example or link to a similar problem that would be extremely helpful.

    <CollectionView ItemsSource="{Binding ????? }">

        <CollectionView.ItemTemplate>

            <DataTemplate>

                    <Grid
                        RowDefinitions="*,*"
                        ColumnDefinitions="3*, *"
                        RowSpacing="5">

                        <Label 
                            Text="{ ????? - HOW TO BIND TO Key in kvp_list }" 
                            Grid.Row="0"
                            Grid.Column="0"
                            BackgroundColor="White"
                            Padding="0,10,30,10"
                            />

                        <Label
                            Text="{Binding HOW TO BIND TO Value in kvp_list ????? }"
                            Grid.Row="1"
                            Grid.Column="0"
                            Grid.ColumnSpan="2"
                            BackgroundColor="LightGrey"
                            Padding="10,10,30,10"
                            Margin="10"
                            />

                    </Grid>

            </DataTemplate>

        </CollectionView.ItemTemplate>

    </CollectionView>

Thanks in advance.

CodePudding user response:

KeyValuePair has two properties, Key and Value that you can use in your bindings

CodePudding user response:

Removing all instances of x:DataType= from the page (as Jason suggested) did the trick -- why? Who knows! It remains a mystery (for me at least) as I found nothing in the official docs that mentioned anything close to this "solution".

The official docs did suggest it is a good thing to use x:DataType= (and most examples in the docs do just that), as this results in compiled bindings which "improve data binding performance" and "compile-time validation of binding expressions enables a better developer troubleshooting experience" -- that is, if you can get it to work!

I have yet to find any official example that makes use compiled bindings for a property such as:

   public List<KeyValuePair<String, String>> kvp_list { ... }

in which the "Key" and "Value" of the KeyValuePair is somehow made available via intellisense and provides compile-time validation. Should someone with far more experience care to post such an example using the "app_data" model in the original post and include the XAML attributes that allow for compiled bindings, be my guest !

  • Related