【问题标题】:Checkbox that doesn't render when clicked单击时不呈现的复选框
【发布时间】:2016-11-22 18:22:43
【问题描述】:

我正在寻找这种行为:

<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>

我在你点击它时 CheckBox 不呈现检查的地方,按钮的原因,因为我希望当用户点击时,视图模型会做一些工作,如果一切正常,那么它会更新 Checkbox 的 IsChecked 标志。

【问题讨论】:

  • 如果您设置IsHitTestVisible="false",则不会触发单个事件并且不会捕获鼠标。你不能点击那个复选框
  • 好的,那么您的问题是什么?请提供一个良好的minimal reproducible example,该minimal reproducible example 可以可靠地重现您遇到的任何特定问题,并清楚、准确地描述该问题是什么。
  • 如何让我的第二个代码示例触发按钮中定义的命令?
  • 我正在努力完成 PaulMolly 从这篇文章中回复的内容:stackoverflow.com/questions/921921/…

标签: c# wpf button checkbox


【解决方案1】:

你的问题是你的模板。这毫无意义。

你可能想做这样的事情:

   <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>

【讨论】:

  • 在您的示例中,您必须单击“单击我”才能触发命令。我希望能够单击“复选框”以触发命令但不呈现检查。
  • @hhherby 更新模板。现在应该按照你的要求去做
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-16
相关资源
最近更新 更多