0
votes

I am using a Listbox in m WPF app where I tried to remove the SelectedItem so the user can reclick on it to do an action.

I have a classic ListBox :

<ListBox
    x:Name="MenuItemList"
    Grid.Row="1"
    IsSynchronizedWithCurrentItem="True"
    ItemsSource="{Binding MenuItems}"
    SelectedItem="{Binding SelectedMenu, UpdateSourceTrigger=PropertyChanged}">
[...] </ListBox>

I have binded the SelectedMenu in my VM :

public MenuItem SelectedMenu
{
    get { return null; }
    set
    {
        MenuIsOpened = false;
        DisplayedMenu = value;

        OnPropertyChanged("SelectedMenu");
    }
}

I tried another way with a private property where I changed it to null

private MenuItem _SelectedMenu;
public MenuItem SelectedMenu
{
    get { return _SelectedMenu; }
    set
    {
        MenuIsOpened = false;
        DisplayedMenu = value;
        _SelectedMenu = null;

        OnPropertyChanged("SelectedMenu");
    }
}

But it does not work as I want... When I click on an item, the property is null but in the view, the listbox always highlights the selected item and the second click on it doesn't trigger the property.

2
"remove the SelectedItem so the user can reclick on it to do an action" - what exactly does that mean? You don't want to remove the item, but just reset the selection? How is that supposed to work? User clicks an item that gets selected, then the selection magically disappears. As a user, I would consider that a bug in your application.Clemens
Said like that it is a bit strange I agree haha. But if you want the explication, the listbox is a menu which is displayed with a button on the right of the app, when the user clicks on an item, the menu disapear and the corresponding view is displayed on the screen. However, I want to remove the selected item to trigger the property changment in my ViewModel and recollapsed the menu if the user click again on the same item. I doesn't need the selection on the item menu as the menu is not visible everytime.Elsassmania
did you get it to work using my answer below?Master Azazel

2 Answers

0
votes

Heres my working example:

    // in constructor or so
    AddedBlocks = new ObservableCollection<string>();

    // the property
    public ObservableCollection<string> AddedBlocks { get; }

    /// <summary>
    /// Deletes selected block from view and model
    /// </summary>
    private void OnDeleteSelectedBlock(object parameter)
    {
        try
        {
            AddedBlocks.RemoveAt(selectedBlockIndex);
        }
    }

And XAML:

<ListBox ItemsSource="{Binding Blocks, Mode=OneWay}" SelectedIndex="{Binding SelectedBlockIndex, Mode=TwoWay}">
        <ListBox.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Delete block" Command="{Binding DeleteSelectedBlock}"/>
            </ContextMenu>
        </ListBox.ContextMenu>
    </ListBox>

See the two way binding on the selectedIndex. Maybe its easier to use that instead of selectedItem. Hope this helps. If you dont want to use the context menu, add a button or this

<KeyBinding Key="Delete" Command="{Binding DeleteSelectedBlock}"/>
0
votes

I do something else to resolve my problem. I didn't try to change the selected item but I added a command on my Listbox

<ListBox
    x:Name="MenuItemList"
    Grid.Row="1"
    ItemsSource="{Binding MenuItems}"
    SelectedItem="{Binding SelectedMenu, UpdateSourceTrigger=PropertyChanged}">
        [...]
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseLeftButtonUp">
            <i:InvokeCommandAction Command="{Binding CloseMenuCommand}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ListBox>

And in my MVVM, I close my menu as I want to do it at the beginning :

private void ExecuteCloseMenuCommand(object o)
{
    MenuIsOpened = false;
}

In this way, the user can reclick on the item menu which is already selected and the menu will still be closed by the click.