tags:

views:

57

answers:

3

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 32x32 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  A: 

It could be that because you're using preview mouse down, that your mouse down event is firing before the listbox recieves it and appropriately sets the correct item to selected. Try switching to mouse left button down instead and seeing if that solves the issue.

Val
Not following. The preview event handling is for a different function, the grid. The listboxitems are set to the selected event. The reason they are there is because the listboxitem handles and stops both right and left mouse button down events.
Ilya
In note 4. you mention that you tie OnSelected to the PreviewMouseLeftButton handler of the ListBoxItem. What might be occurring is that that handler. May be firing before the ListBox has a chance to actually select an item. which is why i thought you might want to try tying to MouseLeftButton instead.
Val
Sorry, note 4 was wrong, corrected it.
Ilya
A: 

Your code does not appear to match up with your explanation. You say the OnSelected method fires as a result of PreviewLeftMouseButtonDown, but your XAML has:

<EventSetter Event="ListBoxItem.Selected" Handler="OnSelected" />

If your OnSelected method is called via a Preview event, your problem makes perfect sense. Preview events are raised before the control has handled them, so the selection hasn't been enacted yet. This gives you a chance to cancel the event, effectively so that it never "reaches" the control.

So with this in mind, you can understand why the SelectedItem is null the first time you select an item - the ListBox hasn't had a chance to apply the selection yet. And subsequent selections will always be one step out for the same reason.

If your event handler relies on selections being enacted then you'll need to ensure they fire after the ListBox has had a chance to apply them. The ListBoxItem.Selected event would suffice for that. Which is why I'm confused about your code.

HTH,
Kent

Kent Boogaart
I fixed my note 4 from PreviewLeftMouse to Selected which is what it really is. Still same issue.
Ilya
In that case, can you add all the code? You're missing the event handlers for the grid preview events.
Kent Boogaart
If you take out the event handlers for the grid preview events still same behavior. Still want me to edit in all the source? Or if you want I can zip up the sln and either post a link (don't know if that's allowed) or can email.
Ilya
A: 

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

Ilya