Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wpf binding to element

Well i guess it's easy my scenario is having 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 IsEnabled property button to be true only if Item from ListBox is selected (focused) in XAML

I tried

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

but no go.

like image 338
kaycee Avatar asked Jul 23 '11 21:07

kaycee


2 Answers

What does the selection have to do with the Touches? (Also the ElementName is off)

I would try this:

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

Explanation: Basically the binding system tries to convert the input to the property at hand, a boolean, so when it gets an integer, 0 will be converter to false, anything higher to true. So the Button will be enabled if one or more items are selected.

like image 169
H.B. Avatar answered Sep 28 '22 03:09

H.B.


<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>
like image 28
sternr Avatar answered Sep 28 '22 04:09

sternr