首页 > 解决方案 > 如何在禁用的 ListboxItem 中启用按钮?

问题描述

我在 Wpf 中有一个 ListBox,里面装满了 ListBoxItems - 每个项目都包含一个文件对象和一个“信息按钮”。这些文件对象包含文件属性 - 例如文件名、路径等 - 但其中一些文件是无效的,由标志标记。

我想禁用包含无效文件的列表框项目,但里面的信息按钮除外。

我成功地在无效元素上禁用了整个列表框项,但这不正确,因为每个文件对象(无论是否无效)都包含特定的文件信息。这可以在单击信息按钮时显示。

此代码运行良好,但结果是列表框项内的每个元素都被禁用。我希望启用该按钮元素。

结果作为图像

和代码:

<ListBox x:Name="fileNameListBox" 
                     ItemsSource="{Binding FileListViewModel.FilteredFileList, Mode=TwoWay}"
                     Grid.Row="4" Grid.Column="0" 
                     Margin="0, 0, 0, 5"
                     HorizontalContentAlignment="Left">

            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding FileListSelectionChangedCommand}"
                                           CommandParameter="{Binding ElementName=fileNameListBox, Path=SelectedItem}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <Label Content="{Binding Filename}" FontWeight="Bold"/>
                        <Label Content="{Binding FilePath}" FontWeight="Light" FontSize="10"/>
                        <Button x:Name="infoButton" Width="50" Content="Info" FontWeight="Light" FontSize="14" HorizontalAlignment="Left"
                                Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:FileListView}}, Path=DataContext.FileListViewModel.FileInfoClickedCommand }"
                                CommandParameter="{Binding}" /> <!-- I want this button to be enabled -->
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>

            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsValid}" Value="False">
                            <Setter Property="IsEnabled" Value="False"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>

所以,我的问题是有没有办法启用这些 ListBox 项中的每个按钮?或者我可以用另一个棘手的解决方案来做这个吗?

标签: c#wpfbuttonlistboxitem

解决方案


推荐阅读