Skip to content
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

Fix IsNullOrEmptyStateTrigger on SelectedItem #4426

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="ListViewStates">
<VisualState x:Name="SelectedItemNotNullState" />
<VisualState x:Name="SelectedItemNullState">
<VisualState.StateTriggers>
<triggers:IsNullOrEmptyStateTrigger Value="{Binding SelectedItem, ElementName=SelectList}"/>
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="RemoveSelection.IsEnabled" Value="False" />
<Setter Target="SelectionEmptyMessage.Visibility" Value="Visible" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
Expand All @@ -43,7 +55,7 @@
<TextBlock x:Name="OurTextBoxError" Text="* Required" Foreground="Red" Margin="10 0 0 0" Visibility="Collapsed" VerticalAlignment="Center" />
</StackPanel>

<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Horizontal" Margin="0,40,0,0">
<Button x:Name="AddButton" Content="Add" Margin="0 0 10 0"/>
<Button x:Name="RemoveButton" Content="Remove"/>
</StackPanel>
Expand All @@ -63,7 +75,20 @@
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<Border BorderBrush="Gold" BorderThickness="1" Margin="0,80,0,0" Width="275">
<StackPanel>
<Button x:Name="RemoveSelection" Content="Deselect" />
<TextBlock x:Name="SelectionEmptyMessage" Text="SelectedItem is empty, select an item" Visibility="Collapsed"
Margin="5,0,0,0"/>
<ListView x:Name="SelectList" HorizontalAlignment="Left">
<ListView.Items>
<TextBlock Text="One" />
<TextBlock Text="Two" />
<TextBlock Text="Three" />
</ListView.Items>
</ListView>
</StackPanel>
</Border>
</StackPanel>

</Grid>
</Page>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public sealed partial class IsNullOrEmptyStateTriggerPage : Page, IXamlRenderLis
private Button _addButton;
private Button _removeButton;
private ListBox _listBox;
private Button _unselectButton;
private ListView _selectListView;

/// <summary>
/// Initializes a new instance of the <see cref="IsNullOrEmptyStateTriggerPage"/> class.
Expand Down Expand Up @@ -52,6 +54,15 @@ public void OnXamlRendered(FrameworkElement control)
}

_listBox = control.FindDescendant("OurList") as ListBox;

_selectListView = control.FindDescendant("SelectList") as ListView;

if (control.FindDescendant("RemoveSelection") is Button btn3)
{
_unselectButton = btn3;

_unselectButton.Click += this.UnselectButton_Click;
}
}

private void AddButton_Click(object sender, RoutedEventArgs e)
Expand All @@ -64,10 +75,18 @@ private void AddButton_Click(object sender, RoutedEventArgs e)

private void RemoveButton_Click(object sender, RoutedEventArgs e)
{
if (_listBox != null)
if (_listBox != null && _listBox.Items.Count > 0)
{
_listBox.Items.RemoveAt(0);
}
}

private void UnselectButton_Click(object sender, RoutedEventArgs e)
{
if(_selectListView != null && _selectListView.SelectedItem != null)
{
_selectListView.SelectedItem = null;
}
}
}
}
14 changes: 10 additions & 4 deletions Microsoft.Toolkit.Uwp.UI/Triggers/IsNullOrEmptyStateTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,28 @@ 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));
dotMorten marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Initializes a new instance of the <see cref="IsNullOrEmptyStateTrigger"/> class.
/// </summary>
public IsNullOrEmptyStateTrigger()
{
SetActive(true);
}

private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var obj = (IsNullOrEmptyStateTrigger)d;
var val = e.NewValue;

obj.SetActive(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)
{
Expand Down