选中复选框时隐藏和显示形状

本文关键字:显示 隐藏 复选框 | 更新日期: 2023-09-27 18:25:05

我正在进行WPF C#项目,并试图通过CheckBox显示我的形状(如果是Checked,则会显示形状)。我做了

Visibility triangle = Visibility.Hidden;        
public MainWindow()
{
    InitializeComponent();
}
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
    Visibility triangle = Visibility.Visible;
}

和XAML:

<Polygon MouseDown="Window_MouseDown" MouseLeftButtonDown="shape_MouseLeftButtonDown" 
    MouseLeftButtonUp="shape_MouseLeftButtonUp" MouseMove="shape_MouseMove" 
    AllowDrop="True" x:Name="triangle" Stroke="Purple" StrokeThickness="2" Canvas.Left="482"
    Canvas.Top="176" Height="30" Width="41">
    <Polygon.Fill> 
        <SolidColorBrush Color="Blue" Opacity="0.4"/> 
    </Polygon.Fill> 
</Polygon> 

它仍然不起作用。是否有其他方法可以在WPF中显示或隐藏形状?或者你能帮我找到代码的问题吗?

选中复选框时隐藏和显示形状

这里你什么都没做。您只需声明一个类型为Visibility的变量,并将其值设置为Visibility.Visible。你看不到你的体型。所以改变这个:

Visibility triangle = Visibility.Visible;

对此:

triangle.Visibility = Visibility.Visible;

虽然我在您的Polygon中看不到Points属性。如果未指定Points,则不会显示。设置Point如下:

<Polygon  AllowDrop="True" x:Name="triangle" Points="50, 100 200, 100 200, 200 300, 30" ...>