-
Hi @dr1rrb. I want to create Custom Control that exposes read only collection as Dependency Property. It seems that there are two problems here: Problem 1: Binding read only collection Dependency Property to CS1660: Cannot convert lambda expression to type 'string' because it is not a delegate type Problem 2: Binding read/write collection Dependency Property to if add the setter (which is wrong I suppose), the code compiles but causes a runtime exception: Microsoft.UI.Xaml.Data.BindingExpression: Error: Failed to apply binding to property [DependencyPropertyDetails(MyStringCollection)] on [CustomControlWithCollection.Controls.MyControl] (ReferenceConverter cannot convert from Uno.Extensions.Reactive.Bindings.BindableListFeed...) Have a look: This is public sealed class MyControl : Control
{
private static readonly MyStyle _defaultStyle = new MyStyle();
public MyControl()
{
DefaultStyleKey = typeof(MyControl);
Style = _defaultStyle;
SetValue(MyStringCollectionProperty, new List<string>()); // initializing local instance of dependency property
SetValue(MyReadOnlyStringCollectionProperty, new List<string>()); // initializing local instance of dependency property
}
#region MyStringCollection
public IList<string> MyStringCollection
{
get { return (IList<string>)GetValue(MyStringCollectionProperty); }
set { SetValue(MyStringCollectionProperty, value); }
}
public static readonly DependencyProperty MyStringCollectionProperty =
DependencyProperty.Register(
nameof(MyStringCollection),
typeof(IList<string>),
typeof(MyControl),
new PropertyMetadata(
new List<string>(), // initializing static instance of dependency property
(s, e) => ((MyControl)s)?.OnMyStringCollectionChanged(e)));
private void OnMyStringCollectionChanged(DependencyPropertyChangedEventArgs e)
{
System.Diagnostics.Debug.WriteLine($"MyProperty Changed from [{((IList<string>)e.OldValue).Count}] to [{((IList<string>)e.NewValue).Count}]");
}
#endregion
#region MyReadOnlyStringCollection
public IList<string> MyReadOnlyStringCollection
{
get { return (IList<string>)GetValue(MyReadOnlyStringCollectionProperty); }
//set { SetValue(MyReadOnlyStringCollectionProperty, value); } // read-only property
}
public static readonly DependencyProperty MyReadOnlyStringCollectionProperty =
DependencyProperty.Register(
nameof(MyReadOnlyStringCollection),
typeof(IList<string>),
typeof(MyControl),
new PropertyMetadata(
new List<string>(), // initializing static instance of dependency property
(s, e) => ((MyControl)s)?.OnReadOnlyMyStringCollectionChanged(e)));
private void OnReadOnlyMyStringCollectionChanged(DependencyPropertyChangedEventArgs e)
{
System.Diagnostics.Debug.WriteLine($"MyProperty Changed from [{((IList<string>)e.OldValue).Count}] to [{((IList<string>)e.NewValue).Count}]");
}
#endregion
}
internal class MyStyle : Style<MyControl>
{
public MyStyle() => Setters(s => s
.BorderBrush(Colors.Red)
.BorderThickness(1)
.Template(ctrl =>
new StackPanel()
.Children(
new ItemsRepeater()
.ItemsSource(x => x.TemplateBind(() => ctrl.MyReadOnlyStringCollection))
.ItemTemplate<string>(s =>
new TextBlock()
.Text(s)),
new ItemsRepeater()
.ItemsSource(x => x.TemplateBind(() => ctrl.MyStringCollection))
.ItemTemplate<string>(s =>
new TextBlock()
.Text(s))
)
));
} This is my consumer: public sealed partial class MainPage : Page
{
public MainPage() => this
.DataContext(new BindableMainPageModel(), (page, vm) => page
.Background(ThemeResource.Get<Brush>("ApplicationPageBackgroundThemeBrush"))
.Content(new StackPanel()
.VerticalAlignment(VerticalAlignment.Center)
.HorizontalAlignment(HorizontalAlignment.Center)
.Children(
new TextBlock()
.Text("Hello Uno Platform!"),
new MyControl()
.MyReadOnlyStringCollection(() => vm.MyStringCollection), // this line causes compilation error
new MyControl()
.MyStringCollection(() => vm.MyStringCollection) // this line causes runtime error
)));
}
internal partial record MainPageModel
{
public IListState<string> MyStringCollection => ListState<string>.Value(this, () =>
[
"Item 1",
"Item 2",
"Item 3",
"Item 4",
"Item 5",
]);
} Project settings:
Tested under Note, that if project runs under Complete Solution: CustomControlWithCollection.zip |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
cc @dansiegel, @dr1rrb |
Beta Was this translation helpful? Give feedback.
Well, it really depends who is the "owner" of the collection. The 2 cases are like fore the Q2:
null
.ObservableCollectio…