Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one group box with header should display a text with button in xaml. setting visibility of Header button using IsbtnVisibility property defined in my viewmodel.
IsbtnVisibility 
is bool property. when it is false the button should collpase but it is not working as expected. Is there any way to solve it.

What I have tried:

<GroupBox Grid.Row="0" Padding="3,5,3,3">

                                              <GroupBox.HeaderTemplate>
                                                  <DataTemplate>
                                                      <StackPanel Orientation="Horizontal" >
                                                          <TextBlock Text="Sample Header " FontWeight="Bold"/>
                                                          <UserCntrl:RoundedButton x:Name="btnRoundedButton" Visibility="{Binding Path=IsbtnVisibility,Converter={StaticResource FalseToCollapsedConverter}}"/>
                                                      </StackPanel>
                                                  </DataTemplate>
                                              </GroupBox.HeaderTemplate>
                                              </GroupBox>
Posted
Updated 9-Oct-18 6:03am

The HeaderTemplate has its own DataContext, so you can't bind directly to properties on the parent DataContext. If you examine the output window, you will see errors telling you that the property was not found.

You need to use a RelativeSource binding to access the parent DataContext:
<UserCntrl:RoundedButton x:Name="btnRoundedButton" 
    Visibility="{Binding Path=DataContext.IsbtnVisibility,
    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}, Converter={StaticResource FalseToCollapsedConverter}}"
/>

WPF GroupBox HeaderTemplate and DataBinding - Stack Overflow[^]
 
Share this answer
 
Comments
kida atlantis 10-Oct-18 4:23am    
It worked like charm..thank you
Do you see a BindingExpression error being displayed in the Output Window when you execute your project in Visual Studio?

c# - WPF Error 40 BindingExpression path error: property not found on 'object' - Stack Overflow[^]

If Yes, that is probably something you want to look into fixing.

If No, You can use a Boolean to Boolean converter in the binding that you are having trouble with. Just set a breakpoint in the converter to see the actual values being passed forth and back between view and data source...

WPF Tutorial | Debug DataBinding Issues[^]
 
Share this answer
 
Comments
kida atlantis 9-Oct-18 5:37am    
I could not see any errors at output window. I am using same property value and bool to control visibility converter.The same logic is working fine but where as groupbox with header the same logic not working fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900