选中CheckBox时隐藏和显示形状

时间:2015-11-30 11:09:50

标签: c# wpf

我正在研究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中显示或隐藏形状的方法吗?或者你可以帮我找到我的代码问题吗?

1 个答案:

答案 0 :(得分:1)

在这里你没有做任何事情。您只需声明Visibility类型的变量,并将其值设置为Visibility.Visible。你没看到你的形状。所以改变这个:

Visibility triangle = Visibility.Visible;

对此:

triangle.Visibility = Visibility.Visible;

虽然我在Points中看不到Polygon属性。如果您未指定Points,则表示未显示。像这样设置Point

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