Constantin Treiber
Constantin Treiber

Reputation: 420

WPF Image Visibility Binding in XAML

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..

    <StackPanel Name="StackPanel1" Grid.Row="3" Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Center">
        <RadioButton GroupName="Group1" Content="1" 
                HorizontalAlignment="Left" 
                Width="35" BorderThickness="1,0,0,1"
                IsChecked="CodeBehindBinding..." />
        <RadioButton GroupName="Group1" Content="2" 
                HorizontalAlignment="Left" VerticalAlignment="Top" 
                Width="35" BorderThickness="1,0,0,1"
                IsChecked="{CodeBehindBinding..." />
    ......

Somewhere else in the XAML i tryied to bind a Label to the group. Together..

    <Image Grid.Row="3" Grid.Column="2" HorizontalAlignment="Left" Height="25" VerticalAlignment="Top" Width="25" Source="/*****;component/Resources/Checked.png" 
           Visibility="{Binding IsChecked, BindingGroupName=StackPanel1.Group1}"
           />

...Nothinig happens. ;-) The Image is permanent visibile.

How can I fix it ? Hope you can help .. Greetz Iki

Upvotes: 2

Views: 13949

Answers (2)

Ofir
Ofir

Reputation: 5319

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

Upvotes: 7

Clemens
Clemens

Reputation: 128146

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}}" ... />

Upvotes: 3

Related Questions