Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何反转WPF自定义控件的颜色_C#_Wpf_Custom Controls - Fatal编程技术网

C# 如何反转WPF自定义控件的颜色

C# 如何反转WPF自定义控件的颜色,c#,wpf,custom-controls,C#,Wpf,Custom Controls,我正在为我的WPF应用程序创建一个自定义控件,我想知道如何在单击该控件时反转该控件的颜色。我已经让它响应鼠标点击,但当我尝试交换背景和前景笔刷时,只有背景颜色改变。该控件是一个模具控件,我希望在选中它时颜色反转。我通过在控制模板中使用网格并将椭圆及其填充笔刷设置为{TemplateBinding Foreground}来创建模具面。任何帮助都将不胜感激。您可以使用像素着色器,例如 它有一个反转颜色,您可以应用它您可以使用像素着色器,例如 它有一个invertcolor,您可以应用它在模板中放

我正在为我的WPF应用程序创建一个自定义控件,我想知道如何在单击该控件时反转该控件的颜色。我已经让它响应鼠标点击,但当我尝试交换背景和前景笔刷时,只有背景颜色改变。该控件是一个模具控件,我希望在选中它时颜色反转。我通过在控制模板中使用网格并将椭圆及其填充笔刷设置为{TemplateBinding Foreground}来创建模具面。任何帮助都将不胜感激。

您可以使用像素着色器,例如


它有一个反转颜色,您可以应用它

您可以使用像素着色器,例如


它有一个invertcolor,您可以应用它

在模板中放置一个触发器,当控件处于选定状态时,该触发器将
{TemplateBinding Foreground}
替换为
“{Binding Background,RelativeSource={RelativeSource TemplatedParent}”
,反之亦然。您不能从setter使用TemplateBinding,因此需要对TemplatedParent的相对资源使用常规绑定。下面是一个带有文本块的复选框示例,该文本块在选中时反转颜色:

<CheckBox Foreground="Blue" Background="LightGreen">
    <ContentControl.Template>
        <ControlTemplate TargetType="CheckBox">
            <TextBlock Name="TextBlock"
                        Background="{TemplateBinding Background}"
                        Foreground="{TemplateBinding Foreground}"
                        Text="Text">
            </TextBlock>
            <ControlTemplate.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter TargetName="TextBlock" Property="Background"
Value="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}"/>
                    <Setter TargetName="TextBlock" Property="Foreground"
Value="{Binding Background, RelativeSource={RelativeSource TemplatedParent}}"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </ContentControl.Template>
</CheckBox>

在模板中放置一个触发器,当控件处于选定状态时,该触发器将用
“{Binding Background,RelativeSource={RelativeSource TemplatedParent}}”替换
{TemplateBinding front}
,反之亦然。您不能从setter使用TemplateBinding,因此需要对TemplatedParent的相对资源使用常规绑定。下面是一个带有文本块的复选框示例,该文本块在选中时反转颜色:

<CheckBox Foreground="Blue" Background="LightGreen">
    <ContentControl.Template>
        <ControlTemplate TargetType="CheckBox">
            <TextBlock Name="TextBlock"
                        Background="{TemplateBinding Background}"
                        Foreground="{TemplateBinding Foreground}"
                        Text="Text">
            </TextBlock>
            <ControlTemplate.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter TargetName="TextBlock" Property="Background"
Value="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}"/>
                    <Setter TargetName="TextBlock" Property="Foreground"
Value="{Binding Background, RelativeSource={RelativeSource TemplatedParent}}"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </ContentControl.Template>
</CheckBox>


谢谢。当我发现我选择的属性必须是从属属性后,它工作得非常好。谢谢。当我发现我选择的属性必须是一个dependencProperty之后,它工作得非常好。