WPF Image Visibility Binding in XAML

大城市里の小女人 提交于 2019-12-20 03:18:35

问题


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


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/20188787/wpf-image-visibility-binding-in-xaml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!