-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix IsNullOrEmptyStateTrigger on SelectedItem #4426
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
7b6794b
fac2b1b
0a7677c
31f1cc4
dae06ea
50a0402
ccb6366
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Specialized; | ||
using Microsoft.Toolkit.Uwp.Helpers; | ||
|
@@ -28,22 +29,47 @@ public object Value | |
/// Identifies the <see cref="Value"/> DependencyProperty | ||
/// </summary> | ||
public static readonly DependencyProperty ValueProperty = | ||
DependencyProperty.Register(nameof(Value), typeof(object), typeof(IsNullOrEmptyStateTrigger), new PropertyMetadata(true, OnValuePropertyChanged)); | ||
DependencyProperty.Register(nameof(Value), typeof(object), typeof(IsNullOrEmptyStateTrigger), new PropertyMetadata(null, OnValuePropertyChanged)); | ||
|
||
/// <summary> | ||
/// Gets or sets a value indicating whether a trigger is active. | ||
/// </summary> | ||
public bool IsActive | ||
{ | ||
get => (bool)GetValue(IsActiveProperty); | ||
set => SetValue(IsActiveProperty, value); | ||
} | ||
|
||
/// <summary> | ||
/// Identifies the <see cref="IsActive"/> DependencyProperty | ||
/// Allows user to enable the trigger from XAML | ||
/// </summary> | ||
public static readonly DependencyProperty IsActiveProperty = | ||
DependencyProperty.Register(nameof(IsActive), typeof(bool), typeof(IsNullOrEmptyStateTrigger), new PropertyMetadata(false, OnIsActiveChanged)); | ||
|
||
private static void OnIsActiveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
if (e.NewValue != null && e.NewValue is bool) | ||
{ | ||
var obj = (IsNullOrEmptyStateTrigger)d; | ||
obj.SetActive((bool)e.NewValue); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. State triggers doesn't use public IsActive properties, but just the base SetActive method.. It's not clear what purpose this one serves? It seems like you can activate this trigger by either setting it to true or the Value to not-null? Which one takes precedence? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When putting in the fix for this issue (#4411), the fix worked except for the initial load of the control/sample page. It's as if the trigger wasn't being evaluated at load time. Hence, the need to at least initially set the trigger to be active. Another hack would have been to just assign Sting.Empty to the SelectedItem in the constructor of the page's code-behind, but I didn't like that approach, and implemented the bool DP instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the default value is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This logic holds, until it doesn't (trigger is not turning On, as expected), which is a condition that was discovered as part of this PR (see the issue with However, by calling Contrast that whole approach, by just simply giving our developers an easy way to set an initial state for the trigger, by exposing a boolean property. I've gone a little further, and exposed this boolean as a dependency property, which allows for easy setting of the property in the XAML. Another approach was the hack (again, mentioned above), of just setting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
How is that artificial? The Value property is
Then don't set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe the problem is that we don't have an analogous In the case this trigger is being used, it should be bound to and either that binding is invalid (which is effectively null) or evaluates to some value. The default state of the trigger being on shouldn't be a problem here between the time the trigger is instantiated, the value property is resolved via binding, and the UI is actually displayed. |
||
|
||
private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
var obj = (IsNullOrEmptyStateTrigger)d; | ||
var val = e.NewValue; | ||
|
||
obj.SetActive(IsNullOrEmpty(val)); | ||
obj.SetActive(obj.IsActive = IsNullOrEmpty(val)); | ||
|
||
if (val == null) | ||
{ | ||
return; | ||
} | ||
|
||
// Try to listen for various notification events | ||
// Starting with INorifyCollectionChanged | ||
// Starting with INotifyCollectionChanged | ||
var valNotifyCollection = val as INotifyCollectionChanged; | ||
if (valNotifyCollection != null) | ||
{ | ||
|
Uh oh!
There was an error while loading. Please reload this page.