2017-03-27 26 views
1

與其他款式的控制,我通常會添加一個矩形背景,以自定義ListBoxItem的風格和定義與Trigger: IsMouseOver=true彩色動畫,但它並沒有在這種情況下工作。如何將懸停效果添加到TListBoxItem?

只有當我設置爲HitTest := True背景矩形,然後懸停動畫作品,但隨後的列表框不會對項目響應點擊,你不能選擇一個項目。

如何添加懸停效果到ListBox?

回答

1

我剛纔碰到了同樣的問題。 我能找到的唯一解決方法是跳過樣式並創建自己的listboxitem。只有你的文字會消失,所以我添加了一個標籤來顯示文字。 它不是很好,但它在我的情況下工作

type 
    TMouseOverListBoxItem = class(TListBoxItem) 
    private 
    FBackGround: TRectangle; 
    FHoverAni: TColorAnimation; 
    FLabel: TLabel; 
    procedure BackgroundClicked(Sender: TObject); 
    protected 
    procedure DoTextChanged; override; 
    public 
    procedure AfterConstruction; override; 
end; 


procedure TMouseOverListBoxItem.AfterConstruction; 
const 
    cStart = TAlphaColorRec.White; 
    cStop = TAlphaColorRec.Yellow; 
begin 
    inherited; 
    // Create background 
    FBackGround := TRectangle.Create(Self); 
    FBackGround.Parent := Self; 
    FBackGround.Fill.Color := cStart; 
    FBackGround.Align := TAlignLayout.Contents; 
    FBackGround.HitTest := True; 
    FBackGround.Sides := []; 
    FBackGround.OnClick := BackgroundClicked; 

    // Create mouse over animation 
    FHoverAni := TColorAnimation.Create(FBackGround); 
    FHoverAni.Parent := FBackGround; 
    FHoverAni.Trigger := 'IsMouseOver=true'; 
    FHoverAni.TriggerInverse := 'IsMouseOver=false'; 
    FHoverAni.StartValue := cStart; 
    FHoverAni.StopValue := cStop; 
    FHoverAni.PropertyName := 'Fill.Color'; 

    // Create label to show text. Background will hide original text 
    FLabel := TLabel.Create(FBackGround); 
    FLabel.Parent := FBackGround; 
    FLabel.Align := TAlignLayout.Client; 
end; 

procedure TMouseOverListBoxItem.BackgroundClicked(Sender: TObject); 
begin 
    if Assigned(OnClick) then 
    OnClick(Self) 
    else if Assigned(ListBox.OnItemClick) then 
    ListBox.OnItemClick(ListBox, Self); 
end; 

procedure TMouseOverListBoxItem.DoTextChanged; 
begin 
    inherited; 
    FLabel.Text := Self.Text; 
end; 
相關問題