<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?
CodePudding user response:
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 eachMonkey.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.
CodePudding user response:
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.