开发者

Strange behavior with CheckBox in DataGridTemplateColumn

I have a SL4 DataGrid with a column containing a checkbox:

   <data:DataGridTemplateColumn Header="Check" CanUserSort="False" >
      <data:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <CheckBox x:Name="CheckRead" Click="CheckRead_Click" IsChecked="{Binding Acknowledged, Mode=TwoWay}" />
        </DataTemplate>
      </data:DataGridTemplateColumn.CellTemplate>
    </data:DataGridTemplateColumn>

Everything works fine as long as the user clicks directly on the checkbox. If they click anywhere else in the cell, the cell gets selected. But on subsequent clicks on the checkbox - the checkbox changes state but that change is not reflected in the bound object.

If the user clicks on any other cell and then goes back to click the checkbox, it works again.

Why does having the cell selected disable the b开发者_如何学Cinding of the checkbox????

I tried the solution proposed of setting horizontal/vertical alignment to stretch but that does not change anything.


Okay, the answer is...

<data:DataGridTemplateColumn Header="Check" CanUserSort="False" >
  <data:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <ContentControl HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" HorizontalAlignment="Center">
        <CheckBox x:Name="CheckRead" Click="CheckRead_Click" IsChecked="{Binding Acknowledged, Mode=TwoWay}"  />
      </ContentControl>
    </DataTemplate>
  </data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>

Wrapping the checkbox in a contentcontrol like this causes the problem to go away!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜