C# 不使用';单击时无法渲染

C# 不使用';单击时无法渲染,c#,wpf,button,checkbox,C#,Wpf,Button,Checkbox,我在寻找这种行为: <Button Command="{Binding Path=CreateButtonCommand}"> <Button.Content> <CheckBox Content="{Binding Path=Name}" IsHitTestVisible="false" IsChecked="{Binding Path=IsChecked, Mode=OneWay}"/>

我在寻找这种行为:

<Button Command="{Binding Path=CreateButtonCommand}">
    <Button.Content>
        <CheckBox Content="{Binding Path=Name}"  IsHitTestVisible="false" 
                  IsChecked="{Binding Path=IsChecked, Mode=OneWay}"/>
    </Button.Content>
</Button>

但我想要这个样子:

<Button Command="{Binding Path=CreateButtonCommand}">
    <Button.Content>
        <CheckBox Content="{Binding Path=Name}"  IsHitTestVisible="false" 
                  IsChecked="{Binding Path=IsChecked, Mode=OneWay}"/>
     </Button.Content>
     <Button.Template>
        <ControlTemplate TargetType="Button">
            <ContentPresenter />
        </ControlTemplate>
     </Button.Template>
</Button>


我想知道的是,当您单击复选框时,复选框不会呈现复选框,这是按钮的原因,因为我希望当用户单击时,视图模型会做一些工作,如果一切正常,那么它将更新复选框的IsChecked标志。

您的问题在于您的模板。这毫无意义

您可能希望执行以下操作:

   <Button Content="Click me" Width="80" Height="20" Command="{Binding CreateButtonCommand}">
                <Button.Template>
                    <ControlTemplate TargetType="Button">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"></ColumnDefinition>
                                <ColumnDefinition Width="*"></ColumnDefinition>
                            </Grid.ColumnDefinitions>

                            <CheckBox Content="{Binding Path=Name}" Grid.Column="0" IsHitTestVisible="false" IsChecked="{Binding Path=IsChecked, Mode=OneWay}"/>
                            <ContentPresenter Grid.Column="1">
                            </ContentPresenter>
                            <Rectangle Fill="Transparent" Opacity="1" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Grid.ColumnSpan="2">
                                <Rectangle.InputBindings>
                                    <MouseBinding Command="{TemplateBinding Command}"></MouseBinding>
                                </Rectangle.InputBindings>
                            </Rectangle>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>


如果设置
ishitestvisible=“false”
则不会触发任何事件,也不会捕获鼠标。你几乎打不到支票箱好吧,那你的问题是什么?请提供一个能够可靠地再现您遇到的任何具体问题的好例子,并清楚、准确地描述该问题是什么。我如何让我的第二个代码示例触发按钮中定义的命令?我正在尝试完成PaulMolly在本文中的回应:在您的例子中,您必须单击“单击我”以启动命令。我希望能够单击“复选框”以启动命令,但不呈现复选框。@hhherby更新的模板。现在应该按您的要求执行