3

我有一个简单的自定义控件,可以向用户显示一条消息(类似于浏览器的信息栏)。

我添加了一个指示错误消息的布尔依赖属性。如果设置了标志,则控件的背景颜色应为红色,否则为黄色。

这是控件的样式(在 Themes\Generic.xaml 中):

<Style TargetType="{x:Type local:InfoBar}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:InfoBar}">
                <ControlTemplate.Triggers>
                    <Trigger Property="IsError" Value="True" >
                             <Setter Property="Background" Value="LightPink" />
                    </Trigger>
                    <Trigger Property="IsError" Value="False" >
                             <Setter Property="Background" Value="LightYellow" />
                    </Trigger>                  
                </ControlTemplate.Triggers>

                <Grid Margin="4,0,4,0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="auto" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{TemplateBinding Message}" Padding="5" FontWeight="Normal" TextWrapping="Wrap" Grid.Column="0"/>
                    <Button x:Name="PART_CloseButton" Grid.Column="1" VerticalAlignment="Top"  >
                        <Button.Template>
                            <ControlTemplate>
                                <Border HorizontalAlignment="Center" VerticalAlignment="Center" BorderBrush="Transparent">
                                    <Image Height="16" Width="16" Source="/QOffice.Common.Controls;component/Images/icons/Close.png"  />
                                </Border>
                            </ControlTemplate>
                        </Button.Template>
                    </Button>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是控件本身:

[TemplatePart(Name = PART_CloseButton, Type = typeof(ButtonBase))]
public class InfoBar : Control
{

    private const string PART_CloseButton = "PART_CloseButton";

    static InfoBar()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(InfoBar), new FrameworkPropertyMetadata(typeof(InfoBar)));

    }

    #region CloseButton

    private ButtonBase _closeButton;
    /// <summary>
    /// Gets or sets the CloseButton template part.
    /// </summary>
    private ButtonBase CloseButton
    {
        get
        {
            return _closeButton;
        }
        set
        {
            if (_closeButton != null)
            {
                _closeButton.Click -= OnButtonClick;
            }

            _closeButton = value;

            if (_closeButton != null)
            {
                _closeButton.Click += OnButtonClick;
            }
        }
    }

    private void OnButtonClick(object sender, RoutedEventArgs e)
    {
        this.Visibility = System.Windows.Visibility.Collapsed;
    }


    #endregion 


    public override void OnApplyTemplate()
    {
        CloseButton = GetTemplateChild(PART_CloseButton) as ButtonBase;


    }


    #region DependencyProperty Message of InfoBar

    public string Message
    {
        get { return (string)GetValue(MessageProperty); }
        set { SetValue(MessageProperty, value); }
    }

    public static readonly DependencyProperty MessageProperty =
        DependencyProperty.Register("Message", typeof(string), typeof(InfoBar),
                new UIPropertyMetadata());

    #endregion


    #region DependencyProperty IsError of InfoBar

    public bool IsError
    {
        get { return (bool)GetValue(IsErrorProperty); }
        set { SetValue(IsErrorProperty, value); }
    }

    public static readonly DependencyProperty IsErrorProperty =
        DependencyProperty.Register("IsError", typeof(bool), typeof(InfoBar),
                new UIPropertyMetadata());

    #endregion





}

如您所见,我已经定义了一个属性 IsError 和一个触发器来设置控件的背景。

但背景总是透明的。除此之外,如果控制功能正常。

怎么了?

4

3 回答 3

3

即使我手动添加背景颜色,您的自定义控件似乎也没有正确设置背景颜色。我不确定这是为什么,希望有人能详细说明。我确实通过使用以下方式更改样式中网格的颜色来解决您的问题:

<Grid.Style>
    <Style TargetType="{x:Type Grid}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:InfoBar}}, Path=IsError}" Value="True">
                <Setter Property="Background" Value="LightPink" />
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:InfoBar}}, Path=IsError}" Value="False">
                <Setter Property="Background" Value="LightYellow" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Grid.Style>

这会根据 InfoBar 控件中的 IsError 值触发网格的背景颜色。

于 2012-07-25T16:55:16.583 回答
1

你给你Control的 aBackground但没有孩子正在使用它。两种可能的解决方案:

  1. TemplateBinding

    <Grid Margin="4,0,4,0" Background="{TemplateBinding Background}">
    
  2. DataTriggerTargetName

    <ControlTemplate TargetType="{x:Type local:InfoBar}">
        <Grid Name="grid" Margin="4,0,4,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="auto" />
            </Grid.ColumnDefinitions>
            ...
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsError" Value="True" >
                <Setter TargetName="grid" Property="Background" Value="LightPink" />
            </Trigger>
            <Trigger Property="IsError" Value="False" >
                <Setter TargetName="grid" Property="Background" Value="LightYellow" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
    

    在此解决方案中,您必须更改顺序:声明ControlTemplate.TriggersGrid

于 2012-07-25T18:04:28.753 回答
0

试试这个(因为 IsError 是 InfoBar 中的 DP,而不是 ControlTemplate 的属性)

<DataTrigger Property="{Binding IsError, RelativeSource={RelativeSource
                                         Mode=TemplatedParent}" Value="True">
      <Setter Property="Background" Value="LightPink" />
</DataTrigger>
<DataTrigger Property="{Binding IsError, RelativeSource={RelativeSource
                                      Mode=TemplatedParent}" Value="False">
      <Setter Property="Background" Value="LightYellow" />
</DataTrigger> 
于 2012-07-25T16:57:47.380 回答