WPF - 将复选框复选标记更改为图像(或隐藏)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21614157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:45:02  来源:igfitidea点击:

WPF - change checkbox checkmark to image (or hide)

wpfimagecheckboxhide

提问by LukaszTaraszka

Can anyone know if there is a simple way to replace checkbox checkmark by image?

谁能知道是否有一种简单的方法可以通过图像替换复选框复选标记?

I have two images for "checked" and "unchecked" checkbox state. I don't know how to use them instead of checkmark.

我有两个图像用于“已选中”和“未选中”复选框状态。我不知道如何使用它们而不是复选标记。

I also tried to replace content with StackPanel and put Image and TextBlock inside. Images are switching by triggers. It works fine but I have no idea how to remove checkmark icon.

我还尝试用 StackPanel 替换内容并将 Image 和 TextBlock 放入其中。图像由触发器切换。它工作正常,但我不知道如何删除复选标记图标。

I googled a lot and found complicated solutions with tons of XAML (BulletDecorator etc). I personaly don't like to complicate my life and I believe there is a simplier solution.

我在谷歌上搜索了很多,发现了大量 XAML(BulletDecorator 等)的复杂解决方案。我个人不喜欢让我的生活复杂化,我相信有一个更简单的解决方案。

Checkbox to change

要更改的复选框

回答by Sheridan

You couldsimply define your own ControlTemplatefor the Checkbox, in which you display your images instead of the default tick. You can find the default ControlTemplatefor the Checkboxin the CheckBox Styles and Templatespage on MSDN.

可以简单地为 定义您自己ControlTemplateCheckbox,在其中显示您的图像而不是默认刻度。你可以找到默认ControlTemplateCheckbox复选框样式和模板上的MSDN页面。

When defining your own ControlTemplate, it's often a good idea to start with the default. Get that working in your project and then you can start tweaking it to your liking. That way, it should keep all the default behaviours that users are used to from a Checkbox.

在定义自己的 时ControlTemplate,从默认值开始通常是个好主意。让它在你的项目中工作,然后你可以开始根据自己的喜好调整它。这样,它应该保留用户习惯的所有默认行为Checkbox

But this is WPF we're talking about here... with a little lateral thinking, it's easily possible to make several controls look likeother controls. For example, here is a ToggleButtonwith a custom ControlTemplatethat makes it look like a Checkbox, but with an Imagethat changes when you click on it instead of the normal tick mark:

但这是我们在这里谈论的 WPF... 稍微横向思考,很容易使几个控件看起来像其他控件。例如,这里ToggleButton有一个自定义ControlTemplate,使它看起来像一个Checkbox,但是Image当你点击它而不是正常的刻度线时,它会发生变化:

<ToggleButton Content="I'm An Image CheckBox Label" Foreground="Black">
    <ToggleButton.Template>
        <ControlTemplate TargetType="{x:Type ToggleButton}">
            <StackPanel Orientation="Horizontal">
                <Image>
                    <Image.Style>
                        <Style>
                            <Setter Property="Image.Source" 
                                Value="/AppName;component/Images/Image1.jpg" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsChecked, 
                                    RelativeSource={RelativeSource AncestorType=
                                    {x:Type ToggleButton}}}" Value="True">
                                    <Setter Property="Image.Source" 
                                        Value="/AppName;component/Images/Image2.jpg" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Image.Style>
                </Image>
                <ContentPresenter Content="{TemplateBinding Content}" 
                    Margin="5,0,0,0" />
            </StackPanel>
        </ControlTemplate>
    </ToggleButton.Template>
</ToggleButton>

It would also be easy to convert this to a CheckBoxas that also uses the IsCheckedproperty... you could probably even just replace all instances of the word ToggleButtonwith the word Checkboxand it should work.

这也将是很容易将此转换为CheckBox作为同样使用该IsChecked财产......你很可能甚至只是替换的单词的所有实例ToggleButtonCheckbox,它应该工作。