开发者

wpf binding to element

开发者 https://www.devze.com 2023-03-22 23:23 出处:网络
Well i guess it\'s easy my scenario is having 2 elements: ListBox and Button: <ListBox Name=\"BannedItemsListBox\"

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"
        MinW开发者_如何学运维idth="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.


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.


<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>
0

精彩评论

暂无评论...
验证码 换一张
取 消