【发布时间】:2014-04-24 14:29:35
【问题描述】:
我正在尝试显示一个具有交替颜色的网格。最好的办法是将模板全部交替,以便能够更改整行。
为了确保当我指的是网格时没有误解,我指的是网格,不是数据网格,不是网格视图,网格如<Grid></Grid>
现在我发现的唯一可行的解决方案是制作一个包含我想要的适当数量行的网格,并且在每一行中我放置另一个网格,其中包含 1 行和 3 列我需要并通过更改为每行复制粘贴背面颜色。如您所见,这不是很干净的解决方案。
所以我环顾四周,发现列表框可以有备用计数,并且在一个简单的触发器上它可以改变一切,这很完美,直到我注意到突出显示不能被禁用。您可以更改高光画笔,但它会覆盖替代颜色,因此两者不能同时使用。在你问是之前,我确实使用了透明灌木,而透明并不是真正透明的,它所做的只是显示 ListBox 控件下方的颜色,该颜色是下方画布的米色,并且项目本身消失了。
任何人都知道在网格上应用交替行模板的方法。在RowDefinition 上放置一个简单的样式会很容易,但是由于您无法真正分辨出您放入网格中的元素类型,我怀疑有没有可以这么简单的事情。
编辑: 这是我最新的变化。有点“干净” 我为每个颜色主题创建了 2 种样式来控制项目,例如
<Style x:Key="PropertyGrid" TargetType="ItemsControl">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<Grid Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"/>
<ColumnDefinition Width="2"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
</Grid>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="AlternatePropertyGrid" TargetType="ItemsControl">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<Grid Background="#FFEBEBEB">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"/>
<ColumnDefinition Width="2"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
</Grid>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
然后我使用堆栈面板并列出每一行
<StackPanel>
<ItemsControl Style="{StaticResource PropertyGrid}">
<Label Content="Identifier" Style="{StaticResource PropertyNameLabel}"/>
<Label Content="{Binding ElementName=cboActions, Path=SelectedItem.UniqueIdentifier}" Style="{StaticResource PropertyValueLabel}"/>
<Rectangle Style="{StaticResource PropertyGridSplitter}"/>
</ItemsControl>
<ItemsControl Style="{StaticResource AlternatePropertyGrid}">
<Label Content="Source" Style="{StaticResource AlternatePropertyNameLabel}"/>
<Label Name="lblSource" Style="{StaticResource AlternatePropertyValueLabel}"/>
<Rectangle Style="{StaticResource AlternatePropertyGridSplitter}"/>
</ItemsControl>
<ItemsControl Style="{StaticResource PropertyGrid}">
<Label Content="Description" Style="{StaticResource PropertyNameLabel}"/>
<Label Name="lblActionDescription" Style="{StaticResource PropertyValueLabel}"/>
<Rectangle Style="{StaticResource PropertyGridSplitter}"/>
</ItemsControl>
</StackPanel>
每个项目在它们各自的样式中都预设了网格列,因此样式决定了项目控件中每个对象在网格中的位置。
它有效,但我仍然觉得可能有一种更有效的方法来做到这一点。这是它现在的截图,可能更有助于视觉人员了解我在这里想要实现的目标。
【问题讨论】:
-
为什么不扩展控件并添加为您执行此操作的 DependencyProperties?
-
可能,但我仍然需要为我计划使用的每种样式创建一个扩展控件。但确实它会使 XAML 更干净。