J4N
J4N

Reputation: 20717

WPF-MVVM: Prevent the user select something else in the listbox

I've a small WPF application(.Net 3.5 :/ ).

In this application I've a listbox, which allows me to select an element to edit on the right part of the application.

If the right part is invalid, I need to prevent the user changing the selection of elements.

I've made a lot of search on this: Some were telling to change the background/brush to make it look like the selection is not possible(but the selection is still possible)

Some other were telling me to update the IsFocusable property of sub elements:

<ListBox itemsSoutces={Binding Test}>
    <ListBoxt.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Focusable" Value="False"/>
        </Style>
    </listBox.ItemContainerStyle>
</ListBox>

The problem with this solution is that this value has to comes from a value of my ViewModel, and I don't know(nor it's possible) to bind the Value of the Style Setter to a property of my ViewModel?

Is it possible? How?

Upvotes: 1

Views: 387

Answers (1)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174319

I would simply disable the listbox if the right part is invalid by setting IsEnabled to false. If your ViewModel contains a corresponding property, you can bind to that:

<ListBox IsEnabled="{Binding IsValid}" ...> ... </ListBox>

Upvotes: 1

Related Questions