c# - 无法在 unifrom 网格中选择单元格
问题描述
我正在开发一个应用程序,我在矩阵(统一网格)中显示图像。当我将光标移到它上面并在鼠标单击时选择图像时,我想聚焦和图像。
XAML:
<ItemsControl Name="UniformGrid" ItemsSource="{Binding ImageList1}" >
<ItemsControl .ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="3" Columns="3" Width="800" Height="500"/>
</ItemsPanelTemplate>
</ItemsControl .ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="0.5">
<Image Source="{Binding Path}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stretch="UniformToFill" />
<!--<TextBlock Background="DimGray" Margin="0,2,0,0" Foreground="White" Height="16" TextAlignment="Center" VerticalAlignment="Bottom">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}x{1}">
<Binding Path="Height"/>
<Binding Path="Width"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>-->
<TextBlock Background="Black" Foreground="White" Height="18" TextAlignment="Center" VerticalAlignment="Center" Margin="0,0,0,4">
<TextBlock Name="ImageName" Margin="0,0,0,1" Foreground="Red" FontWeight="Bold" Text="{Binding FileName}"/>
<TextBlock Name="ImageType" Margin="0,0,0,2" Foreground="LightGoldenrodYellow" FontSize="11">
<TextBlock.Text>
<MultiBinding StringFormat="Type: {0}">
<Binding Path="Extension" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<TextBlock Name="ImageSize" Margin="0,0,0,3" Foreground="Violet">
<TextBlock.Text>
<MultiBinding StringFormat="Size: {0} Bytes">
<Binding Path="Size"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</TextBlock>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl >
代码背后:
int index = 1;
UniformGrid.SelectedItem = UniformGrid.Items[index];
UniformGrid.ScrollIntoView(UniformGrid.Items[index]);
当我将鼠标移到图像上并按下按钮时,我想选择图像。但我有错误说 SelectedItem 在这种情况下不存在。
解决方案
ItemsControl 不处理选择并且没有 SelectedItem 属性。相反,您可以使用带有 ItemsPanel 的 ListBox 作为 UniformGrid,然后您将获得 ListBox 的 SelectedItem。
<ListBox Name="UniformGrid" ItemsSource="{Binding ImageList1}" SelectedItem="{Binding SelectedImage}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="3" Columns="3" Width="800" Height="500"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
..
</DataTemplate>
</ListBox.ItemTemplate>
..
</ListBox>
推荐阅读
- python - 从数组中获取总和为X的数字列表
- css - 选择选项时如何在下拉菜单中设置不透明度?
- java - Java 可变数值文字
- odoo - 通过 sh 脚本安装后无法在浏览器 0.0.0.0:8069 中加载 Odoo 12
- python - 添加文件上传器后,其他按钮重定向到上传器
- android - 根据文本边界将 TextView 放置在容器的中心
- java - 如何删除 SAP Commerce / Hybris 中的 url 属性?
- azure - 如何在 CI 构建 Azure DevOps 中构建单元测试项目以及“Visual Studio 构建”任务
- python - 如何在 python 中获取我的脚本以在 google colab 上使用 GPU?
- xcode12 - 如何在最新的 Xcode 12 beta 中打开新窗口?