【问题标题】:Radio button checked show image1, unchecked show image2 wpf xmal style单选按钮选中显示 image1,未选中显示 image2 wpf xaml 样式
【发布时间】:2014-10-28 06:59:59
【问题描述】:

我正在使用“BooleanToVisibilityConverter”在选中单选按钮时显示一张图像。当未选中此单选按钮时,他们是否可以使用相同的样式显示另一张图片?

<BooleanToVisibilityConverter x:Key="VisibilityConverter" />

<RadioButton Name="rbttest" IsChecked="True"  GroupName="rbtMenuGroup" 
Style="{StaticResource RadioButtonMenuStyle}"  >

<WrapPanel>

<Image Source="/wpf1;component/Images/test1.png"     
Visibility="{Binding IsChecked=false ???}"  Style="{StaticResource MenuIconStyle}" />  

<Image Source="/wpf;component/Images/test2.png" 
Visibility="{Binding IsChecked,  Converter={StaticResource 
VisibilityConverter},ElementName = rbttest}" 
Style="{StaticResource MenuIconStyle}" ></Image>

<TextBlock Text="This is testing" />

</WrapPanel>
</RadioButton>

【问题讨论】:

    标签: wpf


    【解决方案1】:

    我在研究MVVM、DataBinding、DependencyProperty、ControlTemplates之前暂时找到了解决方案。

    我添加了允许反转的 BooleanToVisibilityConverter 类,我的意思是在 true 时隐藏/折叠控件,在 false 时可见!


     using System;
     using System.Windows.Data;
     using System.Windows;
    
     namespace TestBooleanToVisibilityConverter
    {
    class BoolToVisibleOrHidden : IValueConverter
    {
        #region Constructors
        /// <summary>
        /// The default constructor
        /// </summary>
        public BoolToVisibleOrHidden() { }
        #endregion
    
        #region Properties
        public bool Collapse { get; set; }
        public bool Reverse { get; set; }
        #endregion
    
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, 
        System.Globalization.CultureInfo culture)
        {
            bool bValue = (bool)value;
    
                if (bValue != Reverse)
                {
                    return Visibility.Visible;
                }
                else
                {
                    if (Collapse)
                        return Visibility.Collapsed;
                    else
                        return Visibility.Hidden;
                }
        }
    
        public object ConvertBack(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
        {
            Visibility visibility = (Visibility)value;
    
                if (visibility == Visibility.Visible)
                    return !Reverse;
                else
                    return Reverse;
        }
        #endregion
    }
    }
     Now you can reverse this very easily in the XAML.
    
     <local:BoolToVisibleOrHidden x:Key="BoolToVisConverter" Collapse="True" Reverse="True" 
     />
     <local:BoolToVisibleOrHidden x:Key="BoolToVisConverter2" Collapse="True" 
      Reverse="false" />
      ---------------------------------------------------------------------------------
    
    <RadioButton Name="rbttest" IsChecked="True"  GroupName="rbtMenuGroup" 
    Style="{StaticResource RadioButtonMenuStyle}"  >
    
    <WrapPanel>
      <--Show the image when radio button unchecked-->
     <Image Source="/wpf1;component/Images/test1.png"     
     Visibility="{Binding IsChecked,  Converter={StaticResource 
     BoolToVisConverter},ElementName = rbttest}"  Style="{StaticResource MenuIconStyle}"   
     />  
    
      <--Show the image when radio button checked-->
    <Image Source="/wpf1;component/Images/test2.png" 
     Visibility="{Binding IsChecked,  Converter={StaticResource 
     BoolToVisConverter2},ElementName = rbttest}" 
    Style="{StaticResource MenuIconStyle}" ></Image>
    
    <TextBlock Text="This is testing" />
    
    </WrapPanel>
    

    Reference : http://www.rhyous.com/2011/02/22/binding-visibility-to-a-bool-value-in-wpf/
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-26
      • 2014-08-22
      • 2017-10-22
      • 2012-09-05
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多