New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Collection-Type Dependency Properties #1878
Comments
Fiddling around it seems the 'correct' way in UWP is to do something like this:
That avoids sharing the same List reference with all instances. However, it is only safe through the property accessor (which you can make get only). Through binding, it would be possible to set a new list entirely it seems which is why WPF had read-only dependency properties. If you are dealing with an ObservableCollection there are even more complexities this causes because you have to detect the new collection and then hookup event handlers. So, I'm still not sure the 'best' way to do this in UWP. Some clear documentation from Microsoft would be a big help. |
Almost all of our collection properties are get-only for this type of reason. We are looking into opening this up in the future. Generally we have the "default value" be null and then the getter returns a collection. |
Yes, I understand collection properties should be get-only and that follows C# and .net conventions. Therefore, for back-end classes I would normally just write an IList field in the class and expose it as a get-only property. Something like what is below. ObservableCollection<SelectionItem> list = new ObservableCollection<SelectionItem>();
public IList<SelectionItem> SelectedItems
{
get { return list; }
} However, I understand to fully support the binding system/convention (at least in WPF) the field should instead be a dependency property. Therefore, right now what I have in UWP is: public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register(nameof(SelectionBox2.SelectedItems),
typeof(IList<SelectionItem>),
typeof(SelectionBox2),
PropertyMetadata.Create(new CreateDefaultValueCallback(() =>
{
return new List<SelectionItem>();
})));
public IList<SelectionItem> SelectedItems
{
get { return ((IList<SelectionItem>)base.GetValue(SelectedItemsProperty)); }
} As you can see I expose the property as get-only. The issue is that this entire list can still be set by the binding system itself. In other words, while the get-only SelectedItems is 'safe' and follows conventions there is nothing to stop a call to SetValue() by the binding system which would replace the entire list. This is where WPF would have blocked this because the collection-type dependency property would be read-only. So, what I'm asking to clarify: Should collection-types on UI classes be just regular old C# fields with a get-only property accessor? Or should they be some special-case DependencyProperty? (If so, what?) Generally speaking I don't think SelectedItems is bindable in UWP controls anyway so this might be missing in the platform itself? If I understand correctly, your comment below indicates why binding is not supported if a new list is generated in the property getter itself. This is not a good design choice.
I should also add that I'm working on some TemplatedControls. This is a specialized case but should still be covered by the docs in my opinion. |
Ah, thank you for clarifying. @MikeHillberg, thoughts? |
@jevansaks, @MikeHillberg. This also came up in making SelectedItems bindable in a future DataGrid implementation #1500. Is there any talk of fixing this platform-wide so SelectedItems is fully bindable? |
It's certainly a nice-to-have, but I don't know of a formal proposal to do so. |
Created feature-request here: microsoft/microsoft-ui-xaml#3139 |
I'm closing this issue as there does not appear to be a documentation issue to address and corresponding issues have been opened in the WinUI repo. |
* Fixed some suggestions and added redirect for obsolete article * Added some what's new
What is the 'correct' way to support collection dependency properties in UWP? In WPF this was a defined case:
https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/collection-type-dependency-properties
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: