Bind wpf to element

Well, I think it’s easy, my script has 2 elements: ListBox and Button :

<ListBox Name="BannedItemsListBox" Margin="5" MinWidth="100" MaxWidth="100" " Height=" 204" ItemsSource="{Binding Path=BannedItems, Mode=TwoWay}"></ListBox> <Button Name="RemoveBannedItemsButton" Margin="5" MinWidth="65" Height="22" Click="RemoveBannedItemButton_Click">Remove</Button> 

I want to bind the property of the IsEnabled property to true only if the item from the ListBox is selected (focused) in XAML

I tried

 IsEnabled="{Binding ElementName=BannedSourcesListBox, Path=TouchesDirectlyOver.Count}" 

but don't go.

+6
source share
2 answers

What makes a choice with Touches? (Also ElementName off)

I would try this:

 IsEnabled="{Binding SelectedItems.Count, ElementName=BannedItemsListBox}" 

Edit: Apparently, unlike the trigger option, people don't seem to see how this works: basically the binding system tries to convert the input to the property at hand, boolean, so when it gets an integer, 0 will convert to false , anything above to true . Thus, the button will be enabled if one or more items are selected.

+5
source
 <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="138,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click"> <Button.Style> <Style> <Setter Property="Button.IsEnabled" Value="True" /> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=lstTest , Path=SelectedItem}" Value="{x:Null}"> <Setter Property="Button.IsEnabled" Value="False" /> </DataTrigger> </Style.Triggers> </Style> </Button.Style> </Button> 
+3
source

Source: https://habr.com/ru/post/893426/