C# 一排堆垛板
我正在创建一个WPF应用程序,其中包含手动放置在特定位置的C# 一排堆垛板,c#,wpf,background,row,stackpanel,C#,Wpf,Background,Row,Stackpanel,我正在创建一个WPF应用程序,其中包含手动放置在特定位置的100单个StackPanels,并为每个面板指定一个起始背景色。XAML部分如下所示: <StackPanel Height="300" HorizontalAlignment="Left" Margin="228,120,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="6" Background="Red" /> <StackPanel Backgr
100
单个StackPanels
,并为每个面板指定一个起始背景色。XAML部分如下所示:
<StackPanel Height="300" HorizontalAlignment="Left" Margin="228,120,0,0" Name="stackPanel1"
VerticalAlignment="Top" Width="6" Background="Red" />
<StackPanel Background="Red" Height="300" HorizontalAlignment="Left" Margin="234,120,0,0"
Name="stackPanel2" VerticalAlignment="Top" Width="6" />
<StackPanel Background="Red" Height="300" HorizontalAlignment="Left" Margin="240,120,0,0"
Name="stackPanel3" VerticalAlignment="Top" Width="6" />
<StackPanel Background="Red" Height="300" HorizontalAlignment="Left" Margin="246,120,0,0"
Name="stackPanel4" VerticalAlignment="Top" Width="6" />
<StackPanel Background="Red" Height="300" HorizontalAlignment="Left" Margin="252,120,0,0"
Name="stackPanel5" VerticalAlignment="Top" Width="6" />
<Grid x:Name="mainGrid">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Name="stackPanel1" Width="6" />
<StackPanel Grid.Row="1" Height="300" Name="stackPanel2" Width="6" />
<StackPanel Grid.Row="2" Height="300" Name="stackPanel3" Width="6" />
<StackPanel Grid.Row="3" Height="300" Name="stackPanel4" Width="6" />
<StackPanel Grid.Row="4" Height="300" Name="stackPanel5" Width="6" />
</Grid>
如果这行堆叠面板是在设计器中创建并根据上述条件手动放置的单个实体,我如何构造它们
在WPF中,您可以使用
样式来实现此目的:
<Window.Resources>
<Style TargetType="StackPanel">
<Setter Property="Background" Value="Red"></Setter>
</Style>
</Window.Resources>
<Grid>
<StackPanel ... /> <!--You don't need to set background here anymore-->
<StackPanel ... />
....
</Grid>
....
如果不想为同一类型的所有元素指定相同的颜色,可以使用样式的x:键,然后应用于所需的每个StackPanel:
<Style TargetType="StackPanel" x:Key="MyStyle">
然后:
<StackPanel Style="{StaticResource MyStyle}" ... />
编辑:执行此操作的另一个选项如下:
<StackPanel Height="300" HorizontalAlignment="Left" Margin="228,120,0,0" Name="stackPanel1"
VerticalAlignment="Top" Width="6" Background="Red" />
<StackPanel Background="Red" Height="300" HorizontalAlignment="Left" Margin="234,120,0,0"
Name="stackPanel2" VerticalAlignment="Top" Width="6" />
<StackPanel Background="Red" Height="300" HorizontalAlignment="Left" Margin="240,120,0,0"
Name="stackPanel3" VerticalAlignment="Top" Width="6" />
<StackPanel Background="Red" Height="300" HorizontalAlignment="Left" Margin="246,120,0,0"
Name="stackPanel4" VerticalAlignment="Top" Width="6" />
<StackPanel Background="Red" Height="300" HorizontalAlignment="Left" Margin="252,120,0,0"
Name="stackPanel5" VerticalAlignment="Top" Width="6" />
<Grid x:Name="mainGrid">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Name="stackPanel1" Width="6" />
<StackPanel Grid.Row="1" Height="300" Name="stackPanel2" Width="6" />
<StackPanel Grid.Row="2" Height="300" Name="stackPanel3" Width="6" />
<StackPanel Grid.Row="3" Height="300" Name="stackPanel4" Width="6" />
<StackPanel Grid.Row="4" Height="300" Name="stackPanel5" Width="6" />
</Grid>
然后在代码背后:
foreach (var stackpanl in mainGrid.Children.OfType<StackPanel>())
{
if (Grid.GetRow(stackpanl) < 2)
{
stackpanl.Background = new SolidColorBrush(Colors.Blue);
}
}
foreach(mainGrid.Children.OfType()中的var stackpanl)
{
if(Grid.GetRow(stackpanl)<2)
{
stackpanl.Background=新的SolidColorBrush(Colors.Blue);
}
}
按StackPanel包装堆栈面板
<StackPanel x:Name="StackPanelTest" Orientation="Horizontal">
<StackPanel/>
<StackPanel/>
<StackPanel/>
</StackPanel>
是的,没错,先生。但是,我想做的是更改代码(.cs)处的颜色,并且不要为同一类型的所有元素指定相同的颜色。@jquery\u stack…如果不想为同一类型的所有元素指定相同的颜色,可以使用样式的x:Key
,然后应用于所需的每个StackPanel
。有没有办法将它们分组到一个包含代码的单元中?(不是XAML),因此,例如,对第五个stackPanel使用stackPanelsRow[4]等等,而不是使用XAML手动执行?这不是固定的风格。例如,将温度指定给面板47,如果温度大于50ºC,则此面板的颜色必须根据颜色栏值进行更改,但这只能通过code@jquery_stack...it也可以在WPF中使用触发器
,效果更好。您可以在WPF
@jquery\u stack中搜索触发器……但是对于分组样式,我认为最好的方法是使用样式
和x:Key
。如果我只编写StackPanelTest.Children[0],它会工作吗?背景=新的SolidColorBrush(Colors.Blue);没有在代码中声明100个?如果这样做有效,我的问题将得到解决:)