Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1958102
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T07:58:27+00:00 2026-05-17T07:58:27+00:00

Really having an issue debugging this…the reason why is probably very simple but I’m

  • 0

Really having an issue debugging this…the reason why is probably very simple but I’m still an amateur at WPF.

It’s easier if you see the code but I’ll give you the specific problem. My listbox is set to contain a grid format and each listboxitem has a ListBoxItem.PreviewMouseLeftButtonDown event. The problem is, the selected item is always set to the previous item.

Here is what happens in steps:

  1. My image loads up

  2. I click a button to generate a grid overlay of 32×32 cells over the image, each a listboxitem in a cell of that overlay

  3. I click on a cell

  4. OnSelected method fires (each listboxitem is tied to this handler through the Selected event)

  5. The SelectedItem is null when I click on cell (0,0)

  6. If I click on cell (0,1), the Selected event fires but now the SelectedItem is set to the object in the previous cell (0,0) instead of (0,1). Then, clicking on (0,0) will show the SelectedItem as being (0,1) and so on.

  7. Repeat for any other cell.

XAML

 <DockPanel Name="dockTest">
        <Menu DockPanel.Dock="Top" Width="Auto" Height="Auto">
            <MenuItem Header="File">
                <MenuItem Header="Load Image" Command="Open"></MenuItem>
                <Separator />
                <MenuItem Header="Exit" Command="Close"></MenuItem>
            </MenuItem>
        </Menu>
        <ListBox Name="lstTiles" DockPanel.Dock="Right" PreviewMouseRightButtonDown="grdMain_MouseRightButtonDown" 
             PreviewMouseRightButtonUp="grdMain_MouseRightButtonUp"  SelectionMode="Extended">
            <ListBox.ItemContainerStyle>
                <Style>
                <EventSetter Event="ListBoxItem.Selected" Handler="OnSelected" />
                <Setter Property="ListBox.RenderTransformOrigin" Value="0.5,0.5" />
                <Setter Property="Grid.Row" Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                    Path=Content.Row}"/>
                    <Setter Property="Grid.Column" Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                    Path=Content.Column}"/>
                    <Setter Property="ListBoxItem.Height" Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                    Path=Content.Height}" />
                    <Setter Property="ListBoxItem.Width" Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                    Path=Content.Width}" />
                <Setter Property="ListBoxItem.IsHitTestVisible" Value="True" />
                <Style.Resources>
                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue" Opacity=".3" />
                        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
                    </Style.Resources>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <Grid ShowGridLines="True" IsItemsHost="True" Background="{DynamicResource LoadedImage}" 
                      Name="grdMain" MaxHeight="600" MaxWidth="800" MinHeight="600" MinWidth="800" >
                    </Grid>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>
    <StackPanel DockPanel.Dock="Left">
        <Label Margin="5">Tile Size</Label>
        <ComboBox Name="cmbGridSize">
            <TextBlock Name="txt3232">32x32</TextBlock>
            <TextBlock Name="txt1616">16x16</TextBlock>
            <TextBlock Name="txt88">8x8</TextBlock>
        </ComboBox>
        <Button Name="btnChangeGrid" Click="GridBtn_Click" Margin="4">Make Grid</Button>
        <Label>Tile Type</Label>
        <ComboBox Name="cmbTileType" SelectionChanged="cmbTileType_SelectionChanged">

        </ComboBox>
        <TextBlock Name="txtTest"></TextBlock>
        <Label>Tile Characteristic</Label>
        <ComboBox Name="cmbTileCharacteristic" SelectionChanged="cmbTileCharacteristic_SelectionChanged">
        </ComboBox>
        <Button Name="btnExport" Click="btnExport_Click">Export</Button>
        <Label>Edit Cell</Label>
        <Button Name="btnEdit" Click="btnEdit_Click">Edit Cell</Button>
    </StackPanel>
</DockPanel>

Code Behind

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        //Stores enum array of tile type enums
        tileTypes = Enum.GetNames(typeof(Tile.TileType));

        //Stores enum array of tile characteristics
        tileCharacteristics = Enum.GetNames(typeof(Tile.TileCharacteristic));

        //Gets main grid in template of main page
        mainGrid = Helpers.FindItemsPanel(lstTiles) as Grid;

        //Sets combobox to array of types of tiles
        cmbTileType.ItemsSource = tileTypes;

        //Sets combobox to array of characteristics
        cmbTileCharacteristic.ItemsSource = tileCharacteristics;

    }  //Generates the grid overlay
    private void Grid_Build()
    {
        //Sets grid overlay to user preferences of tile size
        if (tileList.Count > 0)
        {
            tileList.Clear();
        }

        if (mainGrid.RowDefinitions.Count > 0)
        {
            mainGrid.RowDefinitions.Clear();
        }

        if (mainGrid.ColumnDefinitions.Count > 0)
        {
            mainGrid.ColumnDefinitions.Clear();
        }

        int numberOfColumns = Convert.ToInt32(Math.Ceiling((float)imageWidth / (float)dimension));
        int numberOfRows = Convert.ToInt32(Math.Ceiling((float)imageHeight / (float)dimension));

        for (int i = 0; i < numberOfRows; i++)
        {
            mainGrid.RowDefinitions.Add(new RowDefinition());
        }

        for (int i = 0; i < numberOfColumns; i++)
        {
            mainGrid.ColumnDefinitions.Add(new ColumnDefinition());
        }

        int addCounter = 0;

        //Stores and generates tile objects in generated rows and columns
        for (int row = 0; row < numberOfRows; row++)
        {
            for (int col = 0; col < numberOfColumns; col++)
            {
                Tile tempTile = (new Tile(row, col, dimension, dimension, addCounter, Tile.TileType.None, Tile.TileCharacteristic.Empty));
                tempTile.IsHitTestVisible = true;
                tileList.Add(tempTile);
                addCounter++;
            }
        }

        //Sets listbox of tiles in xaml to the list of tile objects
        lstTiles.ItemsSource = tileList;
    }

    //Occurs when a listbox item is selected
    private void OnSelected(object sender, RoutedEventArgs e)
    {
        //If a single tile object is selected
        if (lstTiles.SelectedItems.Count == 1)
        {
            //Stores array of selected tile listboxitems in listbox
            var items = lstTiles.SelectedItems;

            foreach (Tile it in items)
            {
                txtTest.Text = it.Row.ToString() + " " + it.Column.ToString();

                //Sets tile type combobox to the property set in each tile object
                cmbTileType.SelectedItem = (string)Enum.GetName(typeof(Tile.TileType), it.Type);

                //Sets tile characteristic combobox to the property set in each tile object
                cmbTileCharacteristic.SelectedItem = (string)Enum.GetName(typeof(Tile.TileCharacteristic), it.Characteristic);
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T07:58:29+00:00Added an answer on May 17, 2026 at 7:58 am

    Problem was Selected was a premature event for getting the listbox’s selecteditems container. What I should have been using was SelectionChanged

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a really bad issue where no matter what I try, the
AKA - What's this obsession with pointers? Having only really used modern, object oriented
This is a really weird problem that I have been having. When I download
I'm having to start building the architecture for a database project but i really
I'm stuck in having to write a simple spam filter I'm not really sure
I'm having a tricky debugging issue, perhaps due to my lack of understanding about
I'm trying to something pretty simple but am having headaches with it. I'm pretty
I think I'm having a really basic problem here but I can't seem to
I am having an issue with collapsing divs in CSS! But not the normal
I'm having a really hard time figuring out the scoping issue with the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.