WPF复选框,如StackOverflow的"已接受答案"复选框

Jud*_*ngo 3 wpf checkbox wpf-controls

我想创建一个自定义复选框,其行为与StackOverflow的"接受答案"复选框完全相同:

alt text http://sstatic.net/so/img/vote-accepted-on.png

alt text http://sstatic.net/so/img/vote-accepted.png

也就是说,我只想在选中时显示单个图像,在未选中时显示单个不同的图像.我不关心不确定的状态.

对于WPF ControlTemplates,我是一个新手,所以我无法自定义CheckbBox的ControlTemplate,以便在选中/取消选中时显示这些图像.有人能指出我正确的方向吗?

ito*_*son 15

这是一个简单的版本:

<ControlTemplate TargetType="CheckBox">
  <Image Name="TickImage" Source="HollowTick.png" />
  <ControlTemplate.Triggers>
    <Trigger Property="IsChecked" Value="True">
      <Setter TargetName="TickImage" Property="Source" Value="FilledTick.png" />
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

这是非常简陋的,因为它不尊重边距和填充等内容,但那些可能对你来说并不重要.关键是当IsChecked为真时使用Trigger和Setter来改变图像源 - 你应该可以从那里建立起来.