Domanda

I need to toggle the canvas visibility from visible to hidden. If I can manually change the canvas' visibility="hidden" and run the program and the canvas is gone, then I would think I'd be able to toggle this with a button. Here is what I have so far. Any help would be appreciated.

XAML

    <Canvas Background="Transparent" Height="200" HorizontalAlignment="Left" Margin="169.5,132.5,0,0" Name="Canvas1" VerticalAlignment="Top" Width="200" Visibility="Visible">
        <Line X1="100" Y1="0" X2="100" Y2="75" Stroke="Red" StrokeThickness="0.95" />
        <!--Top long vertical line> /-->
        <Line X1="100" Y1="95" X2="100" Y2="105" Stroke="Red" StrokeThickness="0.95" />
        <!--Crosshair vertical line> /-->
        <Line X1="100" Y1="125" X2="100" Y2="200" Stroke="Red" StrokeThickness="0.95" />
        <!--Bottom long vertical line> /-->
        <Line X1="0" Y1="100" X2="75" Y2="100" Stroke="Red" StrokeThickness="0.75" />
        <!--Left long horizontal line> /-->
        <Line X1="95" Y1="100" X2="105" Y2="100" Stroke="Red" StrokeThickness="0.75" />
        <!--Crosshair horizontal line> /-->
        <Line X1="125" Y1="100" X2="200" Y2="100" Stroke="Red" StrokeThickness="0.75" />
        <!--Right long horizontal line> /-->
        <!--Ellipse
  Canvas.Top="50"
  Canvas.Left="50"
  Height="100"
  Width="100"
  StrokeThickness="0.75"
  Stroke="Red"/-->
    </Canvas>
    <Canvas Height="73" HorizontalAlignment="Right" Name="Canvas2" VerticalAlignment="Top" Width="69">
        <Image Height="56" Name="Image1" Stretch="Fill" Width="48" Canvas.Left="7" Canvas.Top="8" Source="/HornetView;component/Images/imageedit_6_2746796678.png" />
    </Canvas>
</Grid>
</Window>

VB

Public Sub Button1_Checked_1(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Checked
    Dim chkstat As Integer
    chkstat = 0
    If Button1.IsChecked = True And chkstat = 0 Then
        Canvas1.Visibility = Windows.Visibility.Visible
        chkstat = 1
    Else
        Canvas1.Visibility = Windows.Visibility.Hidden
    End If

End Sub
È stato utile?

Soluzione

Use BooleanToVisibilityConverter and bind ToggleButton.IsChecked to Canvas.Visibility.

<Window.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Window.Resources>

...

<Canvas Name="Canvas1" Visibility="{Binding IsChecked, ElementName=Button1, Converter={StaticResource BooleanToVisibilityConverter}}">
    ...
</Canvas>

Altri suggerimenti

Define your checkstat somewhere else in the class and not in the togglebuttonbutton checked method. In your code you are assigning a value of 'o; to checkstat everytime the togglebuttonbutton is checked so that it never goes into the 'else' part.

Go with the above solution by using a booltovisibility converter and that should fix your problem rather than using private objects in your code behind

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top