C# 无法在unifrom网格中选择单元格

C# 无法在unifrom网格中选择单元格,c#,wpf,xaml,C#,Wpf,Xaml,我正在开发一个应用程序,在这个应用程序中,我以矩阵(统一网格)的形式显示图像。当我在上面移动光标并单击鼠标选择图像时,我想聚焦图像 XAML: 当我将鼠标移到图像上并按下按钮时,我想选择这些图像。 但是我错误地说SelectedItem在此上下文中不存在。ItemsControl不处理选择,并且没有SelectedItem属性。相反,您可以使用带有ItemsPanel作为UniformGrid的ListBox,然后您将获得ListBox的SelectedItem <ListBox Name

我正在开发一个应用程序,在这个应用程序中,我以矩阵(统一网格)的形式显示图像。当我在上面移动光标并单击鼠标选择图像时,我想聚焦图像

XAML:

当我将鼠标移到图像上并按下按钮时,我想选择这些图像。
但是我错误地说SelectedItem在此上下文中不存在。

ItemsControl不处理选择,并且没有SelectedItem属性。相反,您可以使用带有ItemsPanel作为UniformGrid的ListBox,然后您将获得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>

..
..

ItemsControl不处理选择,并且没有SelectedItem属性。相反,您可以使用带有ItemsPanel作为UniformGrid的ListBox,然后您将获得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>

..
..

UniformGrid不包含SelectedItem属性。相反,您可以使用带有ItemsPanel作为UniformGrid的ListBox,然后您将获得SelectedItem。@Aakanksha,如果您将其添加为答案,则可以进行投票并接受:)@petethepagan沙鼠完成。谢谢:)UniformGrid不包含SelectedItem属性。相反,您可以使用带有ItemsPanel作为UniformGrid的ListBox,然后您将获得SelectedItem。@Aakanksha,如果您将其添加为答案,则可以进行投票并接受:)@petethepagan沙鼠完成。谢谢:)
<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>