Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Bind IsEnabled to Listbox SelectedItem

Using MVVM style I have successfully bound an ObservableCollection<string> to a ListBox, showing up the values as RadioButtons. The control behaves exactly as expected.

Now I have an issue regarding some TextBoxes bound to this ListBox: I want whenever the SelectedItem in the ListBox is equal to a specific value (e.g. ValueForEnabled) the TextBoxes to be enabled otherwise they should be disabled.

I know I have to bind to SeletedItem of the ListBox (named lbSource) but how exactly is this done?

I want something like this (Pseudo code):

<TextBox  ...

    IsEnabled="{Binding ElementName=lbSource, Path=SelectedItem='ValueForEnabled',
                Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                ...            
/>
like image 870
Savvas Sopiadis Avatar asked Dec 18 '22 04:12

Savvas Sopiadis


1 Answers

OK! Solved it (in another way) myself! For anyone who wants to know:

<TextBox 
...
usual property definitions
...
                      >
                <TextBox.Style>
                    <Style>
                        <Setter Property="TextBox.IsEnabled" Value="False"/>
                        <Style.Triggers>                              
                            <DataTrigger Binding="{Binding ElementName=lbSource , Path=SelectedItem}" Value="ValueForEnabled">
                                <Setter  Property="TextBox.IsEnabled" Value="true"/>
                            </DataTrigger>                           
                        </Style.Triggers>                       
                    </Style>                   
                </TextBox.Style>
            </TextBox>
like image 59
Savvas Sopiadis Avatar answered Dec 28 '22 22:12

Savvas Sopiadis