0

I have a ListBox and a Button on a Window. When the ListBox has the focus, the IsEnabled property of the Button should be True. Here is what I've tried:

<Style TargetType="{x:Type ListBox}">
    <Setter
        Property="Margin"
        Value="15,0,15,20"></Setter>
        <Style.Triggers>
            <Trigger Property="IsFocused" Value="True" />
            <Setter TargetName="btnActivate" Property="IsEnabled" Value="True" />
        </Style.Triggers>
</Style>

This seems very straightforward but I get an error message that

btnActivate is not recognized.

What is the proper way to do this?

EDIT 1 I implemented MoonMoo's suggested link as follows:

IsEnabled="{Binding ElementName=lstInactive, Path=SelectedIndex, Converter={StaticResource cvtr}}">

using this converter:

Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
        Dim IsFocused As Integer = CInt(value)
        Return If(IsFocused > -1, True, False)
    End Function

This works to turn IsEnabled on when the listbox is focused but does not turn it off when the listbox loses focus. I think maybe I have to look into a multibinding that also binds to the other control that might get the focus.

EDIT 2 I should mention that I tried:

IsEnabled="{Binding ElementName=lstInactive, Path=IsFocused, Converter={StaticResource cvtr}}">

with a converter:

Return CBool(value)

but that did not changed the button's IsEnabled property

SezMe
  • 527
  • 8
  • 24
  • Something similar has been answered here https://stackoverflow.com/questions/14068606/wpf-bind-a-control-visibility-to-the-focused-property-of-another-control. You could bind to the IsEnabled property instead. – Kerri Brown Dec 04 '17 at 13:26
  • @MoonMoo See my edit to the OP. – SezMe Dec 06 '17 at 19:22

1 Answers1

1

you can use DataTrigger for your button instead:

<Button> 
   <Button.Style>
     <Style TargetType="Button">
        <Setter Property="IsEnabled" Value="False"/>
     <Style.Triggers>
        <DataTrigger Binding="{Binding IsFocused,ElementName=YourListBoxName}" Value="True">
           <Setter Property="IsEnabled" Value="True"/>
         </DataTrigger>
     </Style.Triggers>
    </Style>
   </Button.Style>
</Button>
tabby
  • 1,878
  • 1
  • 21
  • 39
  • This is very close but doesn't work. When using IsFocused, the IsEnabled property of the button doesn't change. So I tried Focused (which is the property named in the ListBox documentation, but got a run-time error that that property "is not found". Also, see next comment. – SezMe Dec 06 '17 at 18:50
  • So I tried all other ListBox properties that made any sense and got one to partially work. I tried SelectedIndex with a value of 0 and that worked when I clicked on the first item in the ListBox. So I tried the obvious next step by using a value of "> -1" which did not give a run-time error but also did not change the IsEnabled property of the button. – SezMe Dec 06 '17 at 18:54
  • So you want something that if any of the item gets selected the button should get enabled? – tabby Dec 06 '17 at 19:19
  • No, not quite. I want it to be enabled if the listbox has the focus. I just tried using SelectedIndex as a way of testing various approaches to see if I could get what I want. – SezMe Dec 07 '17 at 19:59