Problem with Trigger / Setter

I am trying to attach a trigger to a menu item so that the checked state of the menu item will affect the visibility of another element.  The menu item and target element are not in templates, but in the main XAML of a window.  The code is below:

<MenuItem x:Name="PeersMenuItem" Header="Peers" IsChecked="True">

<MenuItem.Triggers>

<Trigger Value="True" Property="MenuItem.IsChecked">

<Setter TargetName="PeersControl" Property="UIElement.Visibility"

Value="Visible"/>

</Trigger>

<Trigger Value="False" Property="MenuItem.IsChecked">

<Setter TargetName="PeersControl" Property="UIElement.Visibility"

Value="Collapsed"/>

</Trigger>

</MenuItem.Triggers>

</MenuItem>

The XAML compiles, but when attempting to load the component the run-time complains that Visible is not a valid value for property Visibility.

I do not know if this scenario is supported, but it would be very useful.


Answer this question

Problem with Trigger / Setter

  • RibTickler

    1) i'm not sure.  i just poked around and found no VisibilityConverter as there are for so many others.  could be a bug, or the class is actually not public for some reason.

    2) do something like this:

    <Window.Resources>
    ...
    <local:BoolToVisConverter x:Key="myVisConverter"/>
    ...
    </Window.Resources>

    then in the markup for the UI:

    <CheckBox x:Name="foo">Check Me</CheckBox>

    <Button Visibility="{Binding Path=IsChecked,ElementName=foo,Converter={StaticResource myVisConverter}">Click Me</Button>

  • dean gross

    I have 2 follow on questions:

    1) If there is no default converter why does this work in templates   There are many templates that control the visibility of elements using triggers that are very similar.  The only difference is that I am not in a template, but in the main element tree.

    2) How do I hook up my converter so it is used from XAML

    Michael

  • BFDS.BIZ

    there is no converter built in for visibility.  here are two converter classes i wrote to handle this.  the first is a positive visibility for "true", the second is negative visibility for "true".  you can pass "Hidden" or "Collapsed" to tell it what to do when hiding an object.

    another approach is to have one class and pass as a parameter an operation:

    "POSITIVE_HIDDEN", "POSITIVE_COLLAPSED", "NEGATIVE_HIDDEN", "NEGATIVE_COLLAPSED" and do the right thing from there.




    public
    class YBoolToVisibilityConverter : IValueConverter
    {
       
    public object Convert( object value, Type targetType, object parameter,
            System.Globalization.
    CultureInfo culture )
        {
           
    return System.Convert.ToBoolean(value) Visibility.Visible :
            (((
    string) parameter) == "Hidden" Visibility.Hidden : Visibility.Collapsed);
        }

        public object ConvertBack( object value, Type targetType, object parameter,
            System.Globalization.
    CultureInfo culture )
        {
           
    return null;
        }
    }

    public class YInverseBoolToVisibilityConverter : IValueConverter
    {
       
    public object Convert( object value, Type targetType, object parameter,
            System.Globalization.
    CultureInfo culture )
        {
           
    return System.Convert.ToBoolean(value)
                (((string) parameter) == "Hidden" Visibility.Hidden : Visibility.Collapsed) :
                Visibility.Visible;
        }

        public object ConvertBack( object value, Type targetType, object parameter,
            System.Globalization.
    CultureInfo culture )
        {
           
    return null;
        }
    }


     


  • Problem with Trigger / Setter