标签:
最近在做一个项目的时候,有一个需求就是,通过RadioButton来控制一行内容的显示与不显示,当不显示的时候,下面的项能够占住相应的位置,当增加的时候,又会在原来的位置重新显示,如果使用一般的Grid或者其它的布局的时候,位置就确定下来了,但是使用WrapPanel或者StackPanel这类的控件的时候,能够在增加或者删除项的时候实现重新布局,这在实际使用的时候是非常有用的,现总结如下:
<WrapPanel Grid.Column="1" Grid.Row="1" Orientation="Vertical" HorizontalAlignment="Center">
<StackPanel Orientation="Horizontal" Visibility="{Binding Path=JDBHIsVisibility, Mode=TwoWay,Converter={StaticResource BoolToVisibility
Converter}}">
<Border BorderBrush="Transparent" BorderThickness="2" Height="70">
<Label Name="lbl00" Style="{StaticResource StyleLabel}" HorizontalAlignment="Center" VerticalAlignment="Center" Height="66" Width="300">警单编号:</Label>
</Border>
<Border BorderBrush="Transparent" BorderThickness="2" Height="70" >
<Label Name="lb_id" Content="{Binding Path=JDBH,Mode=TwoWay}" Style="{StaticResource StyleLabel}" HorizontalAlignment="Left" Height="66" Width="715">
</Label>
</Border>
</StackPanel>
<StackPanel Orientation="Horizontal" Visibility="{Binding Path=BJSJIsVisibility, Mode=TwoWay,Converter={StaticResource BoolToVisibilityConverter}}">
<Border BorderBrush="Transparent" BorderThickness="2" Height="70" >
<Label Name="lbl10" Style="{StaticResource StyleLabel}" HorizontalAlignment="Center" VerticalAlignment="Center" Height="66" Width="300">报警时间:</Label>
</Border>
<Border BorderBrush="Transparent" BorderThickness="2" Height="70" >
<Label Name="lb_time" Content="{Binding Path=BJSJ,Mode=TwoWay}" Style="{StaticResource StyleLabel}" HorizontalAlignment="Left" VerticalAlignment="Center" Height="66" Width="715"></Label>
</Border>
</StackPanel>
<StackPanel Orientation="Horizontal" Visibility="{Binding Path=BJDZIsVisibility, Mode=TwoWay,Converter={StaticResource BoolToVisibilityConverter}}">
<Border BorderBrush="Transparent" BorderThickness="2" Height="70" >
<Label x:Name="lbl20" Style="{StaticResource StyleLabel}" HorizontalAlignment="Center" VerticalAlignment="Center" Height="66" Margin="0,8" Width="300" Content="报警地址:"/>
</Border>
<Border BorderBrush="Transparent" BorderThickness="2" Height="70" Width="716" >
<Label x:Name="lb_addr" Content="{Binding Path=BJDZ,Mode=TwoWay}" Style="{StaticResource StyleLabel}" HorizontalAlignment="Left" Height="66" Width="715" Margin="0,-2,-2,-2" />
</Border>
</StackPanel>
<StackPanel Orientation="Horizontal" Visibility="{Binding Path=BJXQIsVisibility, Mode=TwoWay,Converter={StaticResource BoolToVisibilityConverter}}">
<Border BorderBrush="Transparent" BorderThickness="2" Height="70">
<Label Name="lbl30" Style="{StaticResource StyleLabel}" HorizontalAlignment="Center" VerticalAlignment="Center" Height="66" Margin="0,8" Width="300">报警详情:</Label>
</Border>
<Border BorderBrush="Transparent" BorderThickness="2" Height="70" Width="716" >
<Label x:Name="lb_detail" Content="{Binding Path=BJXQ,Mode=TwoWay}" Style="{StaticResource StyleLabel}" HorizontalAlignment="Left" Height="66" Width="715" />
</Border>
</StackPanel>
<StackPanel Orientation="Horizontal" Visibility="{Binding Path=BJRIsVisibility, Mode=TwoWay,Converter={StaticResource BoolToVisibilityConverter}}">
<Border BorderBrush="Transparent" BorderThickness="2" Height="70" >
<Label Name="lbl40" Style="{StaticResource StyleLabel}" HorizontalAlignment="Center" VerticalAlignment="Center" Height="66" Width="300">报 警 人:</Label>
</Border>
<Border BorderBrush="Transparent" BorderThickness="2" Height="70" Margin="0,0,10,0" >
<Label Name="lb_person" Content="{Binding Path=BJR,Mode=TwoWay}" Style="{StaticResource StyleLabel}" HorizontalAlignment="Left" Height="66" Width="715">
</Label>
</Border>
</StackPanel>
<StackPanel Orientation="Horizontal" Visibility="{Binding Path=BJDHIsVisibility, Mode=TwoWay,Converter={StaticResource BoolToVisibilityConverter}}">
<Border BorderBrush="Transparent" BorderThickness="2" Height="70" >
<Label Name="lbl50" Style="{StaticResource StyleLabel}" HorizontalAlignment="Center" VerticalAlignment="Center" Height="66" Width="300" Margin="0,-2">报警电话:</Label>
</Border>
<Border BorderBrush="Transparent" BorderThickness="2" Height="70" Width="719" >
<Label Name="lb_phone" Content="{Binding Path=BJDH,Mode=TwoWay}" Style="{StaticResource StyleLabel}" HorizontalAlignment="Left" Height="66" Width="719" Margin="-2,-2,-2,1"/>
</Border>
</StackPanel>
</WrapPanel>
这里在WrapPanel中嵌套多个StackPanel,注意Visibility="{Binding Path=BJDHIsVisibility, Mode=TwoWay,Converter={StaticResource BoolToVisibilityConverter}}"> 实现的核心是,这里我们定义了一个依赖项属性BJDHIsVisibility,并且和RadioButton的IsChecked属性绑定到一起,当RadioButton选中的时候,IsChecked属性为True,我们不能直接将两个属性绑定到一起,必须去定义一个转换器,将BOOL类型的值转化为Visibility所支持的形,这里需要注意,当RadioButton的IsChecked属性为False的时候,Visibility的属性为Visibility.Hidden,此时WrapPanel会实现重新排列,这是我们需要注意的地方,以后需要的时候可以参考此值!
关于WrapPanel和RadioButton相互配合使用实WrapPanel现动态添加或删除项
标签:
原文地址:http://www.cnblogs.com/seekdream/p/4619402.html