【发布时间】:2017-07-10 10:06:03
【问题描述】:
在 UWP 中,我有 GridView。那个 GridView 有这样的 ItemTemplate:
<Page.Resources>
<DataTemplate x:Key="Template" x:DataType="local:ModelClass">
<local:CustomUserControl
Model="{x:Bind Mode=OneWay}"/>
</DataTemplate>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<GridView x:Name="gvMain" ItemTemplate="{StaticResource Template}" SelectionChanged="gvMain_SelectionChanged">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid Orientation="Vertical"
Margin="0,0,0,-10"
MaximumRowsOrColumns="1"
ItemWidth="50"
ItemHeight="50"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
</Grid>
用户控件是这样的:
<Grid x:Name="gridMain" Width="50" Height="50">
<Grid VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0, 0, -10, 0" Width="20" Height="20" Background="Pink"/>
</Grid>
在代码隐藏中:
public ModelClass Model
{
get { return (ModelClass)GetValue(ModelProperty); }
set { SetValue(ModelProperty, value); SetBackground(); }
}
public static readonly DependencyProperty ModelProperty =
DependencyProperty.Register("Model", typeof(ModelClass), typeof(CustomUserControl), new PropertyMetadata(new ModelClass()));
private void SetBackground()
{
if (Model == null)
{
return;
}
gridMain.Background = Model.BackgroundColor;
}
public CustomUserControl()
{
this.InitializeComponent();
}
我正在像这样填充 GridView:
List<ModelClass> list = new List<ModelClass>();
ModelClass mc = new ModelClass();
mc.BackgroundColor = new SolidColorBrush(Colors.Red);
ModelClass mc1 = new ModelClass();
mc1.BackgroundColor = new SolidColorBrush(Colors.Blue);
ModelClass mc2 = new ModelClass();
mc2.BackgroundColor = new SolidColorBrush(Colors.Yellow);
ModelClass mc3 = new ModelClass();
mc3.BackgroundColor = new SolidColorBrush(Colors.Green);
list.Add(mc);
list.Add(mc1);
list.Add(mc2);
list.Add(mc3);
gvMain.ItemsSource = list;
它看起来是这样的:
每个项目的右上角都有一个小方块,用粉红色着色。
当我单击某个项目时,我希望该项目与其他所有项目重叠,因此我的粉红色方块将可见。
在这种情况下如何更改 GridView 项目的 Z-index?
【问题讨论】:
-
我想澄清一下。您希望粉红色方块在项目未聚焦时部分隐藏,而在聚焦时完全可见。它是否正确?查看用户控件的代码也很有用。只是为了确定您可能有任何可能的页边距或翻译
-
我已经更新了我的问题,添加了用户控件的代码。是的,当项目处于活动状态时,我希望显示粉红色方块,就像 GridView 的最后一个项目中显示的那样。
-
想到并且可能起作用的一件事是篡改 GridViewItem 的模板。玩一下 Focused 视觉状态,看看是否更改 GridViewItem 的 zindex 可以得到你想要的结果想要。