-1

I cannot set the Button.Visibility = Visibility.Collapsed on any of my buttons.

Below is the xaml

           <Button x:Name="createPageButton" Content="Create Page" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="740,75,0,0" Height="61" Width="175" FontSize="24" FontWeight="Bold" Click="CreatePageButton_Click" />
        <Button x:Name="TestButton" Content="Button" HorizontalAlignment="Left" Margin="1699,705,0,0" VerticalAlignment="Top" Visibility="Collapsed"/>

In my code behind I try to set the Visibility property.

            TestButton.Visibility = Visibility.Visible;

I get the following error msg.

Error CS0176 Member 'Visibility.Visible' cannot be accessed with an instance reference; qualify it with a type name instead

Trey Balut
  • 1,355
  • 3
  • 19
  • 39

2 Answers2

2

I cannot reproduce your issue with your code above. It worked well on my side with invoking it for example, inside the click event handle.

private void CreatePageButton_Click(object sender, RoutedEventArgs e)
{
    TestButton.Visibility = Visibility.Visible; 
}

My UWP app version is target build 15063.

Error CS0176 Member 'Visibility.Visible' cannot be accessed with an instance reference; qualify it with a type name instead

But for this error, it seems like you are trying to access static members with instance syntax which is not allowed. Details please reference this similar thread. If you still have issues please upload a minimal reproduced project.

Updated:

If you are using the WindowsTemplateStudio, by default in the template studio blank app, the page doesn't reference the Windows.UI.Xaml namespace (which Visibility class is belong to), so that thrown this error. Just add this reference it will work well.

using Windows.UI.Xaml;
Sunteen Wu
  • 10,509
  • 1
  • 10
  • 21
  • I created a blank UWP test project using the Template Studio blank template and I get the CS0176 error in the Button_click event. But if I usethe UWP blank template with no Template Studio and add a button click, I can see the TestButton.Visibility.=Visibility.Collapsed work. hmm, Other Template Studio apps don't work. – Trey Balut Aug 29 '17 at 03:41
  • @TreyBalut I tested, by default in template studio blank app, the page doesn't reference the `Windows.UI.Xaml` so that thrown this error, I will update the answer. – Sunteen Wu Aug 29 '17 at 06:43
  • Sunteen, Thanks, I added the using Windows.UI.Xaml and it works. A bug in the Template Studio methinks. – Trey Balut Aug 29 '17 at 18:56
0

In my case, the cause was due to a conflict in the imports / using namespaces. It seems that I had two namespaces which have the same naming.

To fix this issue, I had to specify the exact class reference: System.Windows.Visibility

JAD
  • 1,096
  • 12
  • 13