WPF绑定到元素

10

我想这很简单,我的情况是有2个元素:ListBoxButton:

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

我希望将按钮的IsEnabled属性绑定为仅在XAML中选择(聚焦)ListBox中的项时为真。

我尝试了

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

但是没有成功。

2个回答

10

选择操作与触摸有什么关系?(同时ElementName是错误的)

我会试试这个:

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

解释:基本上绑定系统尝试将输入转换为当前属性,一个布尔值,因此当它接收到整数时,0 将被转换为 false,任何更高的数字都将被转换为 true。因此,如果选择了一个或多个项目,则按钮将启用。


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接