在WPF中使用XAML进行动态样式设置

时间:2021-07-30 14:11:00
 G     G     G       B     B     B

 G     G     G       B     B     B

 G     G     G       B     B     B

 R     R     R       Y     Y     Y

 R     R     R       Y     Y     Y

 R     R     R       Y     Y     Y

All of the G B R and Y's above are the individual buttons that are created dynamically using DataTemplate and LIST, and they are arranged inside a WrapPanel. I want to add specific styles to the G, B, R, Y to form a matrixed button list like in the above example. Is there any method to do this using only XAML?

上面的所有G B R和Y都是使用DataTemplate和LIST动态创建的各个按钮,它们被安排在WrapPanel内。我想为G,B,R,Y添加特定样式,以形成矩阵按钮列表,如上例所示。有没有方法只使用XAML来做到这一点?

<ItemsControl x:Name="CategoryPanel" Height="Auto" Width="Auto" FontFamily="Teuton Normal" FontSize="12" Grid.Row="1">
     <ItemsControl.ItemsPanel>
         <ItemsPanelTemplate>
             <WrapPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="Auto" Width="Auto" />
         </ItemsPanelTemplate>
     </ItemsControl.ItemsPanel>
     <ItemsControl.ItemTemplate>
         <DataTemplate>
             <Button Height="55" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Teuton Normal" FontSize="12" Margin="1,1" Padding="3" Click="changeCategory">
                 <TextBlock Text="{Binding Path=name}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" Height="Auto" FontFamily="Teuton Normal" TextWrapping="Wrap" TextAlignment="Center"></TextBlock>
             </Button>
         </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

2 个解决方案

#1


0  

You can use triggers : MSDN

您可以使用触发器:MSDN

#2


0  

You can use triggers, which will be trggiered on the basis of specific value gets matched.

您可以使用触发器,它将根据匹配的特定值进行trggiered。

#1


0  

You can use triggers : MSDN

您可以使用触发器:MSDN

#2


0  

You can use triggers, which will be trggiered on the basis of specific value gets matched.

您可以使用触发器,它将根据匹配的特定值进行trggiered。