【问题标题】:DataGridTemplateColumn: Handling mouse clickDataGridTemplateColumn:处理鼠标点击
【发布时间】:2013-11-17 04:36:37
【问题描述】:

我有一个简单的DataGridTemplateColumn,里面有一个CheckBox

<DataGridTemplateColumn x:Name="dgcAccepted" Width="50">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding Accepted}" IsThreeState="False" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

问题是我的CheckBox 仅在用户单击CheckBox 的“内部”而不是该单元格中的其他任何位置时才会切换。我怎样才能做到这一点?

【问题讨论】:

  • 你没有单元格编辑模板吧?
  • 尝试将 Horizo​​ntalAlignment 和 VerticalAlignment 设置为拉伸。
  • @eranotzap:是的,我没有任何CellEditingTemplate,但没有它它也可以工作。
  • @devhedgehog:这可行,但将复选框与单元格的左上角对齐。设置 Horizo​​ntalContentAlignment 等不会将其带回中心。有什么想法吗?
  • 我说设置为拉伸。你这样做了吗?

标签: .net wpf xaml datagrid datagridtemplatecolumn


【解决方案1】:

您可以使用一个切换按钮并将复选框放入其内容中并绑定属性 IsChecked 女巫 ChekBox。您必须从 togglebutton 中删除样式,使其看起来像没有控件一样平面。

在资源中定义样式

<Style x:Key="PlaneToggleButtonStyle" TargetType="{x:Type ToggleButton}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

并像使用它一样

<DataGridTemplateColumn x:Name="dgcAccepted" Width="50">
<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
       <ToggleButton  Style="{DynamicResource PlaneToggleButtonStyle}" IsChecked="{Binding IsChecked, ElementName=MyCheckedButton}}">
             <CheckBox x:Name="MyCheckedButton" IsChecked="{Binding Accepted}" IsThreeState="False" />
       </ToggleButton>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

【讨论】:

  • 这看起来很有希望。让我试试。 PlaneToggleButtonStyle 在哪里定义?还是我必须在我的 DataGrid 资源中创建一个像 &lt;Style x:Name="PlaneToggleButtonStyle" /&gt; 这样的空样式?
  • 这可行,但 ToggleButton 显示在单元格中,即使我对其应用了空样式也是如此。我想这种风格需要有一些内容。想法?
猜你喜欢
  • 2017-05-25
  • 2012-11-05
  • 1970-01-01
  • 2021-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多