How to bind a Collection of a model to a custom control when the control is expecting a collection of an interface #29285
-
I have a custom control with a bindable property that takes an I've setup an example project at https://github.com/TrueMoniker/Maui_IssueDemo_BindingModelToInterface_Collection. To see my error, run the project and see that the first binding works but the second binding is empty. If anyone can suggest what to try next, I'd appreciate it. I assume I'm making a mistake so I figured I'd try here before reporting it to Issues. For example, say I have a <VerticalStackLayout BindableLayout.ItemsSource="{Binding Source={x:Reference This}, Path=MyLabels}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Label
Text="{Binding MyString}"
VerticalOptions="Center"
HorizontalOptions="Center" />
</DataTemplate>
</BindableLayout.ItemTemplate>
</VerticalStackLayout> MyControl.xaml.cs public static readonly BindableProperty MyLabelsProperty =
BindableProperty.Create(nameof(MyLabels), typeof(ICollection<IMyInterface>), typeof(MyControl), defaultValue: new ObservableCollection<IMyInterface>(), defaultBindingMode: BindingMode.TwoWay);
public ICollection<IMyInterface> MyLabels
{
get => (ICollection<IMyInterface>)GetValue(MyLabelsProperty);
set => SetValue(MyLabelsProperty, value);
} When I try to bind a collection that is explicitly the interface, it works However, I binding the same setup, but with it explicitly as an implementation of the interface, the initial value is ignored. MyModel.cs public interface IMyInterface
{
string MyString { get; set; }
}
public class MyModel : IMyInterface
{
public MyModel(){
}
public MyModel(string mystring){
MyString = mystring;
}
public string MyString { get; set; } = "if you see this, the binding did NOT work";
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I've opened Issue 29337 for this |
Beta Was this translation helpful? Give feedback.
I've opened Issue 29337 for this