如何启用已禁用的ListboxItem中的按钮?

时间:2019-06-23 13:49:46

标签: c# wpf button listboxitem

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

我想禁用列表框项,该列表框项中包含除“信息”按钮之外的无效文件。

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

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

The result as image

和代码:

<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项目中的每个按钮?还是可以用另一个棘手的解决方案做到这一点?

0 个答案:

没有答案