无法在单从网格中执行单元格选择



我正在开发一个应用程序,其中我在矩阵(统一网格(中显示图像。当我将光标移到它上面并在鼠标单击时选择图像时,我想聚焦和图像。

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]);

我想在将鼠标移到图像上并按下按钮时选择图像。 但是我错误地说在这种情况下不存在选定项。

ItemsControl 不处理选择,并且没有 SelectedItem 属性。相反,您可以将ListBox与ItemsPanel一起使用为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>

最新更新