WPF 切换界面的 一些骚操作 根据类型自动生成控件列表,根据item类型生成不同控件的列表

利用Style达到切换界面的目的

同事传授来的,用在一些 控件较少,但是选择情况较多的时候。在后台使用 switch 切换 youType 会使效率更高,在前台切换缺点是,这些style都会被加载。

<ControlTemplate x:Key="youStyle1" TargetType="{x:Type Control}">
   <Button x:Name="btnopen" Height="40" Width="200"   HorizontalAlignment="Left" />
</ControlTemplate>

<ControlTemplate x:Key="youStyle2" TargetType="{x:Type Control}">
   <Button x:Name="btnopen" Height="40" Width="200"   HorizontalAlignment="Left" />
</ControlTemplate>

<ControlTemplate x:Key="youStyle3" TargetType="{x:Type Control}">
   <Button x:Name="btnopen" Height="40" Width="200"   HorizontalAlignment="Left" />
</ControlTemplate>

 

<Control Grid.Column="2">
	<Control.Style>
		<Style TargetType="{x:Type Control}">
			<Style.Triggers>
				<DataTrigger Binding="{Binding youType}" Value="youType1">
					<Setter Property="Template" Value="{StaticResource youStyle1}"/>
				</DataTrigger>
				<DataTrigger Binding="{Binding youType}" Value="youType2">
					<Setter Property="Template" Value="{StaticResource youStyle2}"/>
				</DataTrigger>
				<DataTrigger Binding="{Binding youType}" Value="youType3">
					<Setter Property="Template" Value="{StaticResource youStyle3}"/>
				</DataTrigger>
				<DataTrigger Binding="{Binding youType}" Value="youType4">
					<Setter Property="Template" Value="{StaticResource youStyle4}"/>
				</DataTrigger>
				<DataTrigger Binding="{Binding youType}" Value="youType5">
					<Setter Property="Template" Value="{StaticResource youStyle5}"/>
				</DataTrigger>
				<DataTrigger Binding="{Binding youType}" Value="youType6">
					<Setter Property="Template" Value="{StaticResource youStyle6}"/>
				</DataTrigger>
				<DataTrigger Binding="{Binding youType}" Value="youType7">
					<Setter Property="Template" Value="{StaticResource youStyle7}"/>
				</DataTrigger>
			</Style.Triggers>
		</Style>
	</Control.Style>
</Control>

在使用<ItemsControl>时可以使用列表,需要用到绑定时,将binding 写在style里面,radiobutton在ItemsControl里面是需要联动请添加 GroupName

 

<ItemsControl  ItemsSource="{Binding youItemSouse}" >
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <Control >
	    <Control.Style>
		    <Style TargetType="{x:Type Control}">
			    <Style.Triggers>
				    <DataTrigger Binding="{Binding youType}" Value="youType1">
				    	<Setter Property="Template" Value="{StaticResource youStyle1}"/>
				    </DataTrigger>
			    	<DataTrigger Binding="{Binding youType}" Value="youType2">
			    		<Setter Property="Template" Value="{StaticResource youStyle2}"/>
			    	</DataTrigger>
		    	</Style.Triggers>
	    	</Style>
	   </Control.Style>
     </Control>
   </DataTemplate>
   </ItemsControl.ItemTemplate>
      <ItemsControl.ItemsPanel>
         <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal"/>
         </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
</ItemsControl>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值