首页 > 解决方案 > 无法在 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 在这种情况下不存在。

标签: c#wpfxaml

解决方案


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>

推荐阅读