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:
My image loads up
I click a button to generate a grid overlay of 32x32 cells over the image, each a listboxitem in a cell of that overlay
I click on a cell
OnSelected method fires (each listboxitem is tied to this handler through the Selected event)
The SelectedItem is null when I click on cell (0,0)
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.
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);
}
}
}