We have answer of your question!

100% solved queries, no empty question

Question: Enabling a button in WPF depending on ListBox.SelectedIndex


0

Advertisement


I have a rather classic UI situation - two ListBoxes named SelectedItems and AvailableItems - the idea being that the items you have already selected live in SelectedItems, while the items that are available for adding to SelectedItems (i.e. every item that isn't already in there) live in AvailableItems.

Also, I have the < and > buttons to move the current selection from one list to the other (in addition to double clicking, which works fine).

Is it possible in WPF to set up a style/trigger to enable or disable the move buttons depending on anything being selected in either ListBox? SelectedItems is on the left side, so the < button will move the selected AvailableItems to that list. However, if no items are selected (AvailableItems.SelectedIndex == -1), I want this button to be disabled (IsEnabled == false) - and the other way around for the other list/button.

Is this possible to do directly in XAML, or do I need to create complex logic in the codebehind to handle it?

Question author Rune-jacobsen | Source

Answer


1


Advertisement


Here's your solution.

<Button Name="btn1" >click me    
    <Button.Style>        
        <Style>            
            <Style.Triggers>                
                <DataTrigger 
                     Binding ="{Binding ElementName=list1, Path=SelectedIndex}" 
                     Value="-1">                    
                    <Setter Property="Button.IsEnabled" Value="false"/>                    
                </DataTrigger>                
            </Style.Triggers>            
        </Style>        
    </Button.Style>    
</Button>
Answer author Will

Advertisement


Tickanswer.com is providing the only single recommended solution of the question Enabling a button in WPF depending on ListBox.SelectedIndex under the categories i.e wpf , listbox , . Our team of experts filter the best solution for you.

You may also add your answer!