Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CheckBox behavior difference in a tool bar or as a control

I can get a CheckBox control's isChecked property to a bool property when I place it as a control on a user control or a window. However, if I place it in a Toolbar, it is not even visible except maybe thin left or right edges (but the binding still works). If I put in in a ContextMenu, it appears fine as checked or unchecked, but the binding target is not affected.

This is the ContextMenu definition in Window.Resources:

<ContextMenu x:Key="ContentMenu" DataContext="{Binding MyView}">
  <CheckBox IsChecked="{Binding Path=IsRed}"/>
</ContextMenu>

and this is the StackPanel that contains the CheckBox and the ToolBar with a CheckBox:

<StackPanel Height="20" Orientation="Horizontal" DockPanel.Dock="Top" DataContext="{Binding MyView}">
    <CheckBox IsChecked="{Binding Path=IsRed}"/>

 <ToolBar Margin="10,0,0,0">
<CheckBox Width="18" Height="18" IsChecked="{Binding Path=IsRed}"/>
 </ToolBar>
 </StackPanel>

I would only ask

1) if anyone could suggest a solution to the toolbar issue (a different template, perhaps?),

2) the correct way to define the DataContext of the context menu (which seems to be the actual problem).

like image 511
Hürol Aslan Avatar asked Nov 29 '12 12:11

Hürol Aslan


1 Answers

I see what you mean, the chekbox looks like a toggle button now. If you look at the controltemplate of the toolbar you see it adjusts the controltemplates of some types of child controls.

There is a trick or bug if you will, if you put the Checkbox in some layoutcontainer such as Stackpanel, Grid etc. , then its controltemplate is not adjusted:

<ToolBar>
  <Grid><CheckBox IsChecked="True" VerticalAlignment="Center">Test</CheckBox></Grid>
</ToolBar>
like image 116
Grafix Avatar answered Nov 13 '22 13:11

Grafix