【问题标题】:How to create a custom control with both an enabled and disabled visual state and a content presenter如何创建具有启用和禁用视觉状态以及内容呈现器的自定义控件
【发布时间】:2009-10-12 16:22:35
【问题描述】:

我是制作自定义 WPF 控件的新手,因此非常感谢一些帮助和一些解释。情况如下:

我正在尝试制作具有以下特征的自定义控件:

  • 控件必须有内容展示器。
  • 控件必须具有禁用/启用的视觉状态,或者是三态(未启动/进行中/已完成)
  • 如果可能,控件应支持基于状态(启用/禁用)的动画

理想情况下,我想要一些可以像这样很好地堆叠的东西,所以任何关于如何做到这一点的建议都将不胜感激。

如果有人希望我使用 VSM 东西,我正在使用 Blend 3,但我没有使用 blend 3 扩展的经验,所以如果可能的话,我需要一些指导和解释。感谢您的所有时间!

【问题讨论】:

    标签: c# .net wpf custom-controls


    【解决方案1】:

    在 WPF 中,更改(标准)控件的外观和行为非常容易。在这种情况下,您可以使用复选框作为起点。

    首先复制控件的模板:

    “img43.imageshack.us/img43/1694/36338162.png”

    您将看到以下内容:

    “img194.imageshack.us/img194/374/89409336.png”

    现在将 BulletDecorator 分组到一个 Grid 中,然后将 ContentPresenter 移动到该 Grid 并删除 BulletDecorator 和 ist 子级。

    “img9.imageshack.us/img9/7739/74247109.png”

    此时你有一个类似复选框的控件,它只有一个 ContentPresenter。

    现在您可以使用触发器选项卡来设置行为。

    http://img43.imageshack.us/img43/3675/15279262.png

    代码:

    <Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
    x:Class="WpfApplication1.MainWindow"
    x:Name="Window"
    Title="MainWindow"
    Width="640" Height="480" mc:Ignorable="d">
    
    <Window.Resources>
        <SolidColorBrush x:Key="CheckBoxFillNormal" Color="#F4F4F4"/>
        <SolidColorBrush x:Key="CheckBoxStroke" Color="#8E8F8F"/>
        <Style x:Key="EmptyCheckBoxFocusVisual">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle Stroke="Black" StrokeDashArray="1 2" StrokeThickness="1" Margin="1" SnapsToDevicePixels="true"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="CheckRadioFocusVisual">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle Stroke="Black" StrokeDashArray="1 2" StrokeThickness="1" Margin="14,0,0,0" SnapsToDevicePixels="true"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="CheckBoxStyle1" TargetType="{x:Type CheckBox}">
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="Background" Value="{StaticResource CheckBoxFillNormal}"/>
            <Setter Property="BorderBrush" Value="{StaticResource CheckBoxStroke}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="FocusVisualStyle" Value="{StaticResource EmptyCheckBoxFocusVisual}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type CheckBox}">
                        <Grid x:Name="grid" Width="64.42" Height="15.96">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="13,0,0,0" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="False">
                                <Setter Property="Background" TargetName="grid" Value="#FF07FF00"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter Property="Background" TargetName="grid" Value="Red"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="{x:Null}">
                                <Setter Property="Background" Value="#FFFFDD00"/>
                                <Setter Property="Background" TargetName="grid" Value="#FFFFF400"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    
    <Grid x:Name="LayoutRoot">
        <CheckBox HorizontalAlignment="Left" Margin="117,72,0,0" Style="{DynamicResource CheckBoxStyle1}" VerticalAlignment="Top" Content="CheckBox" IsThreeState="True"/>
        <CheckBox HorizontalAlignment="Left" Margin="117,91.96,0,0" Style="{DynamicResource CheckBoxStyle1}" VerticalAlignment="Top" Content="CheckBox" IsThreeState="True" IsChecked="{x:Null}"/>
        <CheckBox HorizontalAlignment="Left" Margin="117,111.92,0,0" Style="{DynamicResource CheckBoxStyle1}" VerticalAlignment="Top" Content="CheckBox" IsThreeState="True" IsChecked="True"/>
        <CheckBox HorizontalAlignment="Left" Margin="209,72,0,0" VerticalAlignment="Top" Content="CheckBox" IsThreeState="True"/>
        <CheckBox HorizontalAlignment="Left" Margin="209,92.96,0,0" VerticalAlignment="Top" Content="CheckBox" IsChecked="{x:Null}" IsThreeState="True"/>
        <CheckBox HorizontalAlignment="Left" VerticalAlignment="Top" Content="CheckBox" Margin="209,111.92,0,0" IsThreeState="True" IsChecked="True"/>
    </Grid>
    

    这对你有帮助吗?

    您好, 卡尔

    【讨论】:

    • 我实际上已经很清楚这一点,但我想我没有考虑过使用复选框控件来执行此操作。然而,这是一篇非常相关的好帖子,所以我要继续投票,欢迎来到 stackoverflow!
    • 谢谢!让我们再回答一些问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多