Mr.Shark
Mr.Shark

Reputation: 148

Not able to perform selection of cells in unifrom grid

I am working on an application where i am displaying images in a matrix (Uniform grid). I wanted to focus and images when i move my cursor on it and select images on mouse click.

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 >

Code Behind:

int index = 1;
 UniformGrid.SelectedItem = UniformGrid.Items[index];
            UniformGrid.ScrollIntoView(UniformGrid.Items[index]);

I want to select the images when i move mouse over them and button down. But i have error saying SelectedItem does not exist in this context.

Upvotes: 0

Views: 297

Answers (1)

Aakanksha
Aakanksha

Reputation: 349

ItemsControl doesn't handle selection and does not have SelectedItem property. Instead you can use ListBox with ItemsPanel as UniformGrid, then you will get the SelectedItem of ListBox.

<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>

Upvotes: 2

Related Questions