【问题标题】:Custom Control Background Color in WPFWPF中的自定义控件背景颜色
【发布时间】:2018-09-30 18:28:22
【问题描述】:

我创建了一个自定义控件,如下所示 (Generic.xaml):

<Style TargetType="{x:Type local:MyCC}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyCC}">
                <Grid Name="grd" Height="{Binding Height}" Width="{Binding Width}">
                    <Rectangle Name="FirstRec" Fill="Blue"/>
                    <Rectangle Name="SecondRec" Fill="Black" Margin="1"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property = "IsMouseOver" Value = "True">
            <Setter Property = "Background" Value = "Red" />
        </Trigger>
    </Style.Triggers>
</Style>

对于 FirstRec 我想获得 CC 的前景色(而不是蓝色),对于 SecondRec 我需要获得背景色(而不是黑色)。因此,现在触发器无法正常工作!我也不想绑定Grid的Height和Width,因为CC有自己的height和width但是我不知道怎么用!你能帮帮我吗?!

编辑:

它实际上是一个有状态的开关。如果 status == 0 它显示一个空矩形。如果 status == 1 它显示一个填充的矩形。如果状态 == 3 || status == 4 它上面显示一个红十字。这是抄送:

<Style TargetType="{x:Type local:BreakerCC}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:BreakerCC}">
                <Grid Name="grd" Background="{TemplateBinding Background}" >
                    <Rectangle Name="MainRectangle" StrokeThickness="1" Stroke="{TemplateBinding Foreground}"/>
                    <Path Name="Line1" Stroke="Red" StrokeThickness="1" Stretch="Fill">
                        <Path.Data>
                            <LineGeometry StartPoint="0,0" EndPoint="1,1" />
                        </Path.Data>
                        <Path.Style>
                            <Style TargetType="Path">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Path=Status}" Value="0x00">
                                        <Setter Property="Visibility" Value="Hidden"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Path=Status}" Value="0x01">
                                        <Setter Property="Visibility" Value="Hidden"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Path=Status}" Value="0x02">
                                        <Setter Property="Visibility" Value="Visible"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Path=Status}" Value="0x03">
                                        <Setter Property="Visibility" Value="Visible"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Path.Style>
                    </Path>
                    <Path Name="Line2" Stroke="Red" StrokeThickness="1" Stretch="Fill">
                        <Path.Data>
                            <LineGeometry StartPoint="0,1" EndPoint="1,0" />
                        </Path.Data>
                        <Path.Style>
                            <Style TargetType="Path">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Path=Status}" Value="0x00">
                                        <Setter Property="Visibility" Value="Hidden"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Path=Status}" Value="0x01">
                                        <Setter Property="Visibility" Value="Hidden"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Path=Status}" Value="0x02">
                                        <Setter Property="Visibility" Value="Visible"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Path=Status}" Value="0x03">
                                        <Setter Property="Visibility" Value="Visible"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Path.Style>
                    </Path>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是 MainWindow.xaml 中的定义

<Window.Resources>
    <Style x:Key="230KV" TargetType="Control">
        <Setter Property="Background" Value="Black"/>
        <Setter Property="Foreground" Value="Red"/>
    </Style>
    <Style x:Key="132KV" TargetType="Control">
        <Setter Property="Background" Value="Black"/>
        <Setter Property="Foreground" Value="Green"/>
    </Style>
    <Style x:Key="400KV" TargetType="Control">
        <Setter Property="Background" Value="Black"/>
        <Setter Property="Foreground" Value="Purple"/>
    </Style>
</Window.Resources>
<Grid>
    <StackPanel>
        <CC:BreakerCC Status="{Binding Status}" Style="{StaticResource 132KV}" Height="20" Width="20"/>
    </StackPanel>
</Grid>

但当状态为 3 或 4 时,我需要将前景更改为红色,这不起作用!

【问题讨论】:

  • 嗨@RaminRabbani,欢迎来到stackoverflow。您是否有可以发布的错误消息,或者您能否提供一些有关问题所在的详细信息?

标签: c# wpf user-interface custom-controls


【解决方案1】:

为画笔使用 TemplateBindings。根本不需要绑定宽度和高度。

<Style TargetType="local:MyCC">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:MyCC">
                <Grid>
                    <Rectangle Fill="{TemplateBinding Foreground}"/>
                    <Rectangle Fill="{TemplateBinding Background}" Margin="1"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>

请注意,您通常会有一个 Border 元素,而不是一个 Grid,并将 TemplateBindings 分配给它的 Background、BorderBrush 和 BorderThickness 属性 - 默认情况下由 Visual Studio 在您创建新的自定义控件时完成。


编辑:您似乎根本不需要自定义控件。只需像这样创建 ContentControl 样式:

<Window.Resources>
    <Style TargetType="ContentControl" x:Key="BaseStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContentControl">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Path x:Name="path" Stroke="{TemplateBinding Foreground}"
                              Data="{TemplateBinding Content}" Stretch="Fill"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding Path=Status}" Value="0">
                            <Setter TargetName="path"
                                    Property="Visibility" Value="Hidden"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=Status}" Value="1">
                            <Setter TargetName="path"
                                    Property="Visibility" Value="Hidden"/>
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Background" Value="Black"/>
        <Setter Property="BorderBrush" Value="{Binding Foreground,
                                        RelativeSource={RelativeSource Self}}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Width" Value="20"/>
        <Setter Property="Height" Value="20"/>
    </Style>
    <Style x:Key="230KV" TargetType="ContentControl" BasedOn="{StaticResource BaseStyle}">
        <Setter Property="Foreground" Value="Red"/>
    </Style>
    <Style x:Key="132KV" TargetType="ContentControl" BasedOn="{StaticResource BaseStyle}">
        <Setter Property="Foreground" Value="Green"/>
    </Style>
    <Style x:Key="400KV" TargetType="ContentControl" BasedOn="{StaticResource BaseStyle}">
        <Setter Property="Foreground" Value="Purple"/>
    </Style>
</Window.Resources>
<StackPanel>
    <ContentControl Style="{StaticResource 230KV}">
        <PathGeometry>M0,0 L1,1 M0,1 L1,0</PathGeometry>
    </ContentControl>
    <ContentControl Style="{StaticResource 132KV}">
        <PathGeometry>M0,0 L1,1 M0,1 L1,0</PathGeometry>
    </ContentControl>
    <ContentControl Style="{StaticResource 400KV}">
        <PathGeometry>M0,0 L1,1 M0,1 L1,0</PathGeometry>
    </ContentControl>
</StackPanel>

【讨论】:

  • 谢谢。但我无法将两个矩形添加到边框,这就是我将其更改为网格的原因。
  • 您实际上不需要两个 Rectangle 。 Border 元素有一个 Background 和一个 BorderBrush,这会产生相同的视觉外观,但 BorderThickness 设置为 1。
  • 你能帮我做触发器吗?我将“背景”更改为“前景”,但它不起作用。如果我改变高度或宽度,它可以正常工作,但我不知道为什么我不能改变前景!
  • 您可能在实例化控件时明确设置了 Foreground。用样式设置器替换它。你也可以解释你最终想要达到的目标。它只是一个带有填充和边框颜色的矩形,还是还有更多?
  • 好的,我把它贴在这里!谢谢。
猜你喜欢
  • 2011-01-01
  • 2014-09-04
  • 2011-04-10
  • 1970-01-01
  • 2011-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多