wp7 控制控件显隐

xaml:  

xmlns:Converters="clr-namespace:TestControl"

    <phone:PhoneApplicationPage.Resources>

        <Converters:BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    </phone:PhoneApplicationPage.Resources>

            <Image Source="/TestControl;component/images/image_gifmark.png" Width="30" Height="20" Visibility="{Binding SEX, Converter={StaticResource BoolToVisConverter}}" Margin="46,434,380,55" />
            <Button Content="Vis" Height="72" HorizontalAlignment="Left" Margin="82,422,0,0" Name="button3" VerticalAlignment="Top" Width="160" Click="button3_Click"/>
            <Button Content="Col" Height="72" HorizontalAlignment="Left" Margin="258,420,0,0" Name="button4" VerticalAlignment="Top" Width="160" Click="button4_Click"/>

cs:

namespace TestControl
{
    public class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var boolValue = System.Convert.ToBoolean(value);

            if (parameter != null)
                boolValue = !boolValue;

            return boolValue ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value.Equals(Visibility.Visible);
        }
    }
}

    public class model : INotifyPropertyChanged
    {
        private bool _sex { get; set; }
        public bool SEX
        {
            get
            {
                return _sex;
            }
            set
            {
                _sex = value;
                RaisePropertyChanged("SEX");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyname));

            }

        }
    }

        model mymodel = new model() { SEX = false };

        this.DataContext = mymodel;

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            mymodel.SEX = true;
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            mymodel.SEX = false;
        }

原文地址:https://www.cnblogs.com/androllen/p/2922004.html