选中单选按钮显示image1,未选中show image2 wpf xaml style

时间:2014-10-27 04:08:13

标签: wpf

我正在使用“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>

1 个答案:

答案 0 :(得分: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/