带按钮的列表视图

时间:2018-04-20 17:16:22

标签: c# wpf listview button binding

我有一个列表视图,每行都有按钮,但是当我将IsEnabled属性绑定到SelectedIndex时仍然保持启用所有按钮,我只需要这个按钮,而且还有另一个必须启用的按钮取决于列表视图中对象的属性。

例如我有一个具有bool HasLckFile属性的类File,我需要在用户上传之前保持禁用下载按钮,但绑定不起作用。

XAML:

<ListView x:Name="lv" Style="{StaticResource CustomListViewStyle}" Margin="470,0,0,0" SelectionMode="Multiple" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" >
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox Tag="{Binding Filename}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Width="150">
                        <GridViewColumn DisplayMemberBinding="{Binding Filename}"/>
                    </GridViewColumn>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="download" VerticalAlignment="Center" Click="btnDownload">
                                    <Button.Style>
                                        <Style TargetType="{x:Type Button}">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding ElementName=lv, Path=SelectedIndex}" Value="-1">
                                                    <Setter Property="IsEnabled" Value="False"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Button.Style>
                                </Button>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="upload" VerticalAlignment="Center" Click="btnUpload">
                                    <Button.Style>
                                        <Style TargetType="{x:Type Button}">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding ElementName=lv, Path=SelectedIndex}" Value="-1">
                                                    <Setter Property="IsEnabled" Value="False"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Button.Style>
                                </Button>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView.Columns>
            </GridView>

        </ListView.View>
    </ListView>

0 个答案:

没有答案