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 issue with setting SelectedItem on TreeView would crash application #3136

Merged
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
20 changes: 20 additions & 0 deletions dev/TreeView/APITests/TreeViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,26 @@ public void TreeViewBackgroundTest()
});
}

[TestMethod]
public void VerifySettingSelectionDoesNotCrash()
{
RunOnUIThread.Execute(() =>
{
var treeview = new TreeView();

var items = new ObservableCollection<string>() {
"Item0", "Item1", "Item2"
};

treeview.SelectedItem = items[0];
Verify.AreEqual(treeview.SelectedItem, items[0]);

treeview.ItemsSource = items;
treeview.SelectedItem = items[1];
Verify.AreEqual(treeview.SelectedItem, items[1]);
});
}

private bool IsMultiSelectCheckBoxChecked(TreeView tree, TreeViewNode node)
{
var treeViewItem = tree.ContainerFromNode(node) as TreeViewItem;
Expand Down
2 changes: 1 addition & 1 deletion dev/TreeView/TreeView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ void TreeView::OnPropertyChanged(const winrt::DependencyPropertyChangedEventArgs
{

const auto items = SelectedItems();
const auto selected = items.Size() > 0 ? items.GetAt(0) : nullptr;
const auto selected = (items != nullptr && items.Size() > 0) ? items.GetAt(0) : nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

items != nullptr [](start = 31, length = 16)

Under what circumstances is this null?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that SelectedItems returns null when TreeView can't resolve it's listcontrol.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and what causes that to happen?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SelectedItems returns null if the internal listview can't be found or the listviews ListViewModel can't be found. So in #3129, we probably crashed due to the binding resolving before the template was applied leaving us in a state where we did not have a listview or selectionmodel.

// Checking if new value is different to the currently internally selected item
if (args.NewValue() != selected)
{
Expand Down