Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do this in XAML?

I want to do this in XAML with a trigger, how do I do it?

    If ListBox1.SelectedIndex > -1 Then
        Border1.Visibility = Windows.Visibility.Visible
    Else
        Border1.Visibility = Windows.Visibility.Hidden
    End If

This XAML code does NOT work. SelectedIndex member is not valid because it does not have a qualifying type name.

            <ListBox.Triggers>
                <Trigger SourceName="ListBox1" Property="SelectedIndex" Value="False">
                    <Setter TargetName="Border1" Property="Visibilty" Value="Hidden" />
                </Trigger>
            </ListBox.Triggers>
like image 661
Rick Rat Avatar asked Nov 20 '25 09:11

Rick Rat


1 Answers

Can you show me how are you trying to do this in xaml?

In case of this error message you need to mention type name also with the property inside trigger.

<Trigger SourceName="ListBox1" Property="ComboBox.SelectedIndex" Value="-1">
    <Setter TargetName="Border1" Property="Border.Visibility" Value="Hidden" />
</Trigger>

Further, it seems that you are adding a Trigger in <ListBox.Triggers> collection, but you can only add EventTrigger in to this collection. So you need to declate a Style for your ListBox to add a Trigger for that and your Border element should be inside the ControlTemplate of ListBox, but in your case Border seems to be outside of ListBox, so declaring a style will not be a solution. Instead you should use Binding with SelectIndex property with the help of a ValueConverter(say IndexToVisibilityConverter). You need to define this converter in codebehind and add it in resources.

<Border Visibility={Binding Path=SelectedIndex, ElementName=ListBox1, Converter={StaticResource IndexToVisibilityConverter}}/>

Choice is totally on your requirements.

like image 175
viky Avatar answered Nov 22 '25 00:11

viky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!