0
votes

i have an issue with selectedItem of a listbox. When I select an item of the listbox, a popup would be displayed where you click the add button to select an image (it contains a value of selectedItem) which is working fine. But after clicking the add button to select the image, then you realise the image is wrong, so you click the add button again to select another image, it started problem because selectedItem is null. How to handle it? How to stay the value of selectedItem? Your given code much appreciated.

if (lstDinner.SelectedItem != null)
{
  output = _imageInserter.InsertImage(imageName, lstDinner.SelectedItem.ToString());
  PopupToysImage.IsOpen = true;
  strDinner.DinnersDetails = lstDinner.SelectedItem.ToString()

}

else
{
 // strDinner.DinnersDetails = null that cause a problem.
 output = _imageInserter.InsertImage(imageName, strDinner.DinnersDetails);
 PopupDinnerImage.IsOpen = true;
 }

UPDATE HERE:

WPF:

<ListBox Style="{DynamicResource ListBoxStyle1}"  DisplayMemberPath="Dinner" BorderBrush="#FFF0F0F0"  x:Name="lstDinner" FontSize="20" HorizontalAlignment="Left" Margin="0,110,0,72.667" Width="436" SelectionMode="Extended"  PreviewMouseLeftButtonDown="MouseDownHandler"  ScrollViewer.CanContentScroll="True" UseLayoutRounding="False" KeyDown="lstDinner_KeyDown" MouseDoubleClick="lstDinner_MouseDoubleClick" >

events in C#:

    private void MouseDownHandler(object sender, MouseButtonEventArgs e)
    {
        var parent = (ListBox)sender;

        _dragSource = parent;

        var data = GetObjectDataFromPoint(parent, e.GetPosition(parent));

        if (e.ChangedButton == MouseButton.Left && e.ClickCount == 1)
        {
            if (data != null)
                DragDrop.DoDragDrop(parent, data, DragDropEffects.Move);
        }
    }


    private void lstDinner_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Delete)
        {
            RemoveItemsFromDatabase();
        }
    }

        private void lstDinner_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        _dinnerImage = new DinnerImageExtractor();
        BitmapImage getImage = new BitmapImage();

        if (lstDinner.SelectedItem != null)
        {
            getImage = _dinnerImage.GetDinnerImages(lstDinner.SelectedItem.ToString());

            if (getImage != null)
            {
                DinnerImagePopup.Source = getImage;
            }
            else
            {
                DinnerImagePopup.Source = new BitmapImage(new Uri("/DinnerApplicationWPF;component/Menu/Images/noImage-icon-pink.png", UriKind.Relative));

            }

            PopupDinnerImage.IsOpen = true;
          //  PopupInstrcution.IsOpen = false;

        }
    }
3
Try to issue a refresh after the failed operation. That will load the values in dropdown box.Hammad Khan
Issue a bind operation on any control that has binding on it. If dropdown has bidning issue this mydropdown.bind()Hammad Khan
i am not using dropdown but only using listbox. can you please show me your code example if u dont mind....user235973457
why are you testing null two times? lstDinner.SelectedItem != null || lstDinner.SelectedItem == nulHammad Khan
i just update my post. please look at it....user235973457

3 Answers

0
votes

I would suggest something like this

if ( lstDinner.SelectedItem == null)
{
  output = _imageInserter.InsertImage(imageName, lstToys.SelectedItem.ToString());
  PopupToysImage.IsOpen = true;
  lstDinner.Databind();
}

Note: This may not work as I dont have your actual code. I have added DataBind() in the if statement, if the selected item was null. It should refresh the list.

0
votes

Best thing is to use two different Listbox item templates for selected and unselected items. So without displaying popup, you can add button into the selected item template.

0
votes

Are you disabling the ListBox while you select the image?

If so I believe by simply disabling the ListBox the SelectedItem will be set to null.

EDIT:

I imagine you want your event handlers (like the mouse double click) to happen when an item in your list is double clicked, not when the ListBox is double clicked. You need to change your XAML to this:

    <ListBox Style="{DynamicResource ListBoxStyle1}" DisplayMemberPath="Dinner" BorderBrush="#FFF0F0F0"  x:Name="lstDinner" FontSize="20" HorizontalAlignment="Left" Margin="0,110,0,72.667" Width="436" SelectionMode="Extended"  PreviewMouseLeftButtonDown="MouseDownHandler"  ScrollViewer.CanContentScroll="True" UseLayoutRounding="False" KeyDown="lstDinner_KeyDown">
        <ListBox.Resources>
            <Style TargetType="ListBoxItem">
                <EventSetter Event="MouseDoubleClick" Handler="lstDinner_MouseDoubleClick" />
            </Style>
        </ListBox.Resources>
    </ListBox>

My selected item does not come up null when I run this code.