WPF Image Visibility Binding in XAML

后端 未结 2 513
野的像风
野的像风 2021-01-21 17:28

I’ve got a little problem binding an image to a Radiobuttion. I only want to bind it via XAML an what I did is this.. I createad a Stackpanel with 5 radiobutton in it..

相关标签:
2条回答
  • 2021-01-21 18:02

    I assume that "IsChecked" property is Boolean. In order to bind Visibility property you must bind to Visibility type property or use converter.

    If you don't want to use converter you need to declare Visibility notification property:

        private Visibility isChecked= Visibility.Visible;
        public Visibility IsChecked
        {
            get { return isChecked; }
            set
            {
                isChecked = value;
                RaisePropertyChanged("IsChecked");
            }
        }
    

    If you want to stay with your Boolean parameter, use Visibility converter

    0 讨论(0)
  • 2021-01-21 18:16

    Two things wrong here. First, you need to assign a Name to the RadioButton you want to use as binding source and use that for the binding's ElementName property.

    <RadioButton x:Name="radioButton1" ... />
    

    Then your binding also needs a converter from bool to Visibility. You could use WPF's BooleanToVisibilityConverter:

    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </Window.Resources>radia
    
    <Image Visibility="{Binding IsChecked, ElementName=radioButton1,
                        Converter={StaticResource BooleanToVisibilityConverter}}" ... />
    
    0 讨论(0)
提交回复
热议问题