-1

I need help as i've a UserControl, "myUC" and i want change its visibility when i press a MenuItem. I've following class BoolToVisibilityConverter :

  [ValueConversion(typeof(bool), typeof(Visibility))]
public sealed class BoolToVisibilityConverter : IValueConverter
{
    public Visibility TrueValue { get; set; }
    public Visibility FalseValue { get; set; }

    public BoolToVisibilityConverter()
    {
        // set defaults
        TrueValue = Visibility.Visible;
        FalseValue = Visibility.Collapsed;
    }

    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        if (!(value is bool))
            return null;
        return (bool)value ? TrueValue : FalseValue;
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        if (Equals(value, TrueValue))
            return true;
        if (Equals(value, FalseValue))
            return false;
        return null;
    }
}

I am setting my converter as StaticResource in Windows.Resource

<Window.Resources>
    <vs:BoolToVisibilityConverter x:Key="VisibilityConverter" TrueValue="Visible" FalseValue="Hidden" />
</Window.Resources>

And i've added converter when i call user control in XAML

<uc:Add Visibility="{Binding MyProperty,Converter={StaticResource VisibilityConverter},FallbackValue=Hidden}" />.

I've binding on the property " MyProperty" so:

   private bool _myProperty;

    public bool MyProperty
    {
        get { 
            return _myProperty; 
        }
        set {
            if (_myProperty== true)
                _myProperty= value;
            else
                _myProperty= true;
            OnPropertyChanged("MyProperty");

        }
    }

But when i launch the application, it does not change the visibility of UserControl.

I forgot to mention that i've the MenuItem bindig with ICommand

    <MenuItem Name="mnuAggiungi" Header="_Aggiungi" Command="{Binding MyCommand}"/>

    RelayCommand _add;

    public ICommand MyCommand
    {
        get
        {
            if (_add == null) _add = new RelayCommand(param => this.MyCommandUC());
            return _add;
        }
    }

   public void  MyCommandUC()
    {
    }

I forgot to mention that i've the MenuItem bindig with ICommand

MenuItem Name="mnuAggiungi" Header="_Aggiungi" Command="{Binding MyCommand}"

    RelayCommand _add;
    public ICommand MyCommand
    {
        get
        {
            if (_add == null) _add = new RelayCommand(param => this.MyCommandUC());
            return _add;
        }
    }
   public void  MyCommandUC()
    {
     }
Dansi
  • 41
  • 4

2 Answers2

0

in command handler MyCommandUC do this..

public void MyCommandUC
{
    MyProperty = false; // for hidden
}

but saying this I would want to tell you that, your code is quite confusing, sometimes you set visibility to Hidden sometimes you set it to Collapsed, also, you try to set truevalue and FalseValues from xaml but your converter has hard coded values in its constructor there by making it useless to set anything from xaml

--- edit

I just saw this .. and I.. !

set {
        if (_myProperty== true)
            _myProperty= value;
        else
            _myProperty= true;
        OnPropertyChanged("MyProperty");

    }

change above code to

set {
        _myProperty= value;
        OnPropertyChanged("MyProperty");
    }
Muds
  • 4,006
  • 5
  • 31
  • 53
0

Value of MyProperty change when click on MenuItem, and i capture click with ICommand. I've think to do so

      _private bool _myProperty;

...

     set
        { _
      myProperty = value; 
               }
      }

    public void  MyCommandUC()
          {
        if (MyProperty == false)
            MyProperty = true;
        else
            MyProperty = false;
         }

but don't work

Dansi
  • 41
  • 4