2017-03-27 14 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; 
関連する問題