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

时间:2012-12-25 06:33:04

标签: c# wpf triggers

我正在尝试执行以下操作:一旦我的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" />

1 个答案:

答案 0 :(得分:4)

您可以在按钮中使用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>