组合矩形图像,如果我隐藏矩形,图像也应该隐藏

时间:2012-01-09 21:01:48

标签: c# .net wpf xaml layout

我正在使用一个矩形并将情感图像放入其中,我想要做的是如果我隐藏了矩形,则应隐藏其中的情感图像。 我正在附上一张图片以寻求帮助。 enter image description here

请告诉我应该使用哪个属性来获取此属性,如果我们使用面板,则可以在Windows窗体中自动完成。但是在WPF C#中,这不是自动完成的。

这是代码

<Rectangle Grid.ColumnSpan="2" Height="71" HorizontalAlignment="Left" Margin="226,262,0,0" Name="rectangle2" Stroke="Black" VerticalAlignment="Top" Width="192" Fill="#B5101010" Visibility="Hidden" />
<Image Height="27"  HorizontalAlignment="Left" Margin="229,266,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="28" Source="/WPFTEST;component/Images/emo/emotion_evilgrin.png" MouseUp="image1_MouseUp_1" Visibility="Hidden" />
<Image Height="27" HorizontalAlignment="Left" Margin="264,266,0,0" Name="image2" Source="/WPFTEST;component/Images/emo/emotion_grin.png" Stretch="Fill" VerticalAlignment="Top" Width="28" MouseUp="image2_MouseUp" Visibility="Hidden" />
<Image Height="27" HorizontalAlignment="Left" Margin="34,299,0,0" Name="image4" Source="/WPFTEST;component/Images/emo/emotion_tongue.png" Stretch="Fill" VerticalAlignment="Top" Width="28" Grid.Column="1" MouseUp="image4_MouseUp" Visibility="Hidden" />
<Image Height="27" HorizontalAlignment="Left" Margin="68,266,0,0" Name="image5" Source="/WPFTEST;component/Images/emo/emotion_suprised.png" Stretch="Fill" VerticalAlignment="Top" Width="28" Grid.Column="1" MouseUp="image5_MouseUp" Visibility="Hidden" />
<Image Height="27" HorizontalAlignment="Left" Margin="34,266,0,0" Name="image6" Source="/WPFTEST;component/Images/emo/emotion_smile.png" Stretch="Fill" VerticalAlignment="Top" Width="28" Grid.Column="1" MouseUp="image6_MouseUp" Visibility="Hidden" />
<Image Height="27" HorizontalAlignment="Left" Margin="0,266,0,0" Name="image7" Source="/WPFTEST;component/Images/emo/emotion_happy.png" Stretch="Fill" VerticalAlignment="Top" Width="28" Grid.Column="1" MouseUp="image7_MouseUp" Visibility="Hidden" />
<Image Height="27" HorizontalAlignment="Left" Margin="0,299,0,0" Name="image8" Source="/WPFTEST;component/Images/emo/emotion_wink.png" Stretch="Fill" VerticalAlignment="Top" Width="28" Grid.Column="1" MouseUp="image8_MouseUp" Visibility="Hidden" />
<Image Height="27" HorizontalAlignment="Left" Margin="230,299,0,0" Name="image9" Source="/WPFTEST;component/Images/emo/emotion_unhappy.png" Stretch="Fill" VerticalAlignment="Top" Width="28" MouseUp="image9_MouseUp" Visibility="Hidden" />
<Image Height="27" HorizontalAlignment="Left" Margin="265,299,0,0" Name="image10" Source="/WPFTEST;component/Images/emo/emotion_waii.png" Stretch="Fill" VerticalAlignment="Top" Width="28" MouseUp="image10_MouseUp" Visibility="Hidden" />

我知道它不在矩形标签下,但如果我在矩形标签下添加图像则会导致错误。

由于 与Atif

2 个答案:

答案 0 :(得分:3)

如果这些图标是“矩形”的一部分,应该是ItemsControl或某种类型的面板,如果父容器被隐藏,图标隐藏({{3} } = Visibility)。

编辑:图片不是矩形的一部分,只是使用可怕的边距滥用代码放置在顶部。

矩形和图标之间没有关系。正如我所说,图标需要作为容器的子项添加。 (例如Visibility.Hidden/Collapsed ItemsControlWrapPanel)如果图标不是拥有容器的一部分,肯定不会隐藏图标...

答案 1 :(得分:0)

如果您希望保留代码而不必更改这些边距,则只需将图标的Visibility属性绑定到矩形的Visibility属性即可。

Visibility =“{Binding Visibility,ElementName = rectangle2}”