简体   繁体   中英

Binding visibility to isChecked value of a checkbox

I'm trying to bind my stackpanel's visibility to the checkbox's isChecked value. It's a common problem, but I just can't figure it out.

Converter:

<Page.Resources>
    <common:BooleanToVisibilityConverter x:Key="BoolToVis"/>
</Page.Resources>

StackPanel I want to mess with(I removed some code, so don't you worry about listview's binding, I set it in c#):

<ListView x:Name="aktualniGracze" ItemsSource="{Binding}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel x:Name="stackLiczymy" Visibility="{Binding isChecked,  ElementName=czyLiczymy, Converter={StaticResource BoolToVis}}">
                <TextBlock Text="{Binding ileWypil}"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

CheckBox:

 <CheckBox Name="czyLiczymy"/>

Nothing happens when I change the state of the checkbox, any clues?

Thanks a lot in advance.

If you have your correct DataContext in CheckBox, I think you are just missing the DataContext.isChecked

<ListView x:Name="aktualniGracze" ItemsSource="{Binding}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel x:Name="stackLiczymy" Visibility="{Binding DataContext.isChecked,  ElementName=czyLiczymy, Converter={StaticResource BoolToVis}}">
                <TextBlock Text="{Binding ileWypil}"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM