Unable to access Metro XAML custom element from code

Trying to use a solution for this question gives me a strange problem. (Replicated here for convenience)

This is a solution to the ListView problem by swallowing the right-click event and preventing the AppBar from opening, a class that inherits the ListView and overrides the OnRightTapped ListViewItem event:

 public class MyListView : ListView { protected override DependencyObject GetContainerForItemOverride() { return new MyListViewItem(); } } public class MyListViewItem : ListViewItem { protected override void OnRightTapped(Windows.UI.Xaml.Input.RightTappedRoutedEventArgs e) { base.OnRightTapped(e); e.Handled = false; // Stop 'swallowing' the event } } <CustomControls:MyListView x:Name="ItemsList" ... SelectionMode="Single" RightTapped="MyListView_RightTapped"> </CustomControls:MyListView> 

I implemented a custom control, as indicated, in a new namespace called CustomControls, exactly as described. I added that the namespace in MainPage.xaml

 xmlns:CustomControls="using:CustomControls" 

When I then try to reference the “ItemsList” in the code behind, I get a compilation error

 The name 'ItemsList' does not exist in the current context 

I tried building, rebuilding, clearing the solution, closing and reopening the solution, putting the classes in the main project namespace, but to no avail.

To summarize, MainPage.cs does not see the user control on MainPage.xaml

UPDATE 2: Submitted a question to remove irrelevant issues. I also changed the name to reflect the real problem.

+4
source share
1 answer

I used Name , but had to use x:Name . Apparently, user controls and user controls should use x:Name , not Name , so that they can be seen in the code.

Read more about the difference here:

In WPF, what are the differences between x: Name and Name attributes?

+9
source

Source: https://habr.com/ru/post/1437548/


All Articles