触发listview的IsSelected属性(初学者)

本文关键字:初学者 属性 IsSelected listview 触发 | 更新日期: 2023-09-27 17:49:24

我想做以下事情:一旦我的listview IsSelected,我想将我的按钮的IsEnabled属性设置为true。

这是我尝试过的,但是我得到了以下异常:

'Initialization of 'System.Windows.Controls.ListView' threw an exception.' Line number '25' and line position '19'.

代码如下:

 <ListView ItemsSource="{Binding ReferenceCollection}" SelectedItem="{Binding SelectedReference}" Grid.ColumnSpan="2" Name="ListView1"  >
        <ListView.Triggers>
            <Trigger Property="ListView.IsSelected" Value="True">
                <Setter TargetName="okBtn" Property="Button.IsEnabled" Value="True" />
            </Trigger>
        </ListView.Triggers>
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Label Content="{Binding ReferenceName}" />
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

按钮:

 <Button IsEnabled ="False" Grid.Row ="2" Content="OK" Name="okBtn" Click="addReference_Click" />

触发listview的IsSelected属性(初学者)

你可以在Button中使用DataTrigger:

<ListView Name="YourList">
        <ListViewItem Content="1234" />
        <ListViewItem Content="1234" />
        <ListViewItem Content="1234" />
    </ListView>
    <Button Content="OK">
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding SelectedItem, ElementName=YourList}"
                                 Value="{x:Null}">
                        <Setter Property="IsEnabled"
                                Value="False" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>