Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger listview's IsSelected property (Beginner)

Tags:

c#

wpf

triggers

I'm trying to do the following: once my listview IsSelected, I want to set my button's IsEnabled property to true.

Here's what I've tried, but I'm getting the following exception:

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

Here's the code:

 <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>

and Button:

 <Button IsEnabled ="False" Grid.Row ="2" Content="OK" Name="okBtn" Click="addReference_Click" />
like image 228
idish Avatar asked Dec 25 '12 06:12

idish


1 Answers

You can use DataTrigger in the Button:

<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>
like image 101
Ramin Avatar answered Oct 15 '22 17:10

Ramin