2013-05-14 2 views
0

런타임에 XE4 FM Stringgrid에 스타일을 적용하려고하지만 올바른 구문을 찾을 수 없습니다.런타임에 Delphi XE4 Firemonkey StringGrid 셀에 스타일 적용

StringGrid는 이미 디자인 타임에 만든 'TextCellStyle'(기본값)을 상속하고이 스타일에 따라 문자열 격자에 셀을 표시합니다.

내가 이상적으로 할 일은 런타임에 특정 셀 (음수 = 빨강, 양수 = 녹색 등)의 글꼴 색을 변경하는 것이지만, 어떻게 할 수 없는지는 잘 모르겠다. 셀 수준에서 스타일을 확인합니다.

이 쿼리는 런타임시 그리드에 메모리를 동적으로 할당해야하기 때문에 TGrid가 아닌 TStringGrid와 관련이 있으며 문자열 그리드로 수행하는 것이 훨씬 쉽습니다.

어떤 도움을 주시면 감사하겠습니다. 미리 감사드립니다.

+0

TGrid를 사용하면 쉽습니다. TStringGrid를 사용하면 불가능하거나 매우 어렵습니다 (아직 결정하지 않았습니다). –

답변

0

TGrid를 사용하여이 작업을 수행하는 방법을 배우려고 시도했으며 Mike Sutton의 도움 덕분에이를 관리했습니다.

[배경 Changing TTextCell background colour at runtime XE4를 참조하십시오.]

는이 권리를 얻는 것을 처리하는 데, 나는 TStringGrid에 비슷한 논리를 시도하고 괜찮 았는데. stringgrid는 Grid1GetValue를 사용하지 않으므로 단순히 FormCreate의 격자에 난수를 하드 코딩했습니다.

[아래 코드에는 textcellstyle이라는 스타일이 있습니다. textcellstyle의 '배경'구성 요소에 TRectangle을 추가 했으므로 TRectangle의 'Fill'속성을 호출 할 수있었습니다. . 아직 스타일의 상단에 아직, 그것은 도움이 경우, 상기와

내 코드를 링크를 참조하십시오

Procedure TForm1.FormCreate(Sender : TObject); 

    begin 
     { CREATE AN EXTRA COLUMN } 
     StringGrid1.AddObject(TFinancialColumn.CreateStringGrid1)); 
     { HARD-CODE THE ROWS } 
     StringGrid1.Cells[0,0] :='0'; 
     StringGrid1.Cells[0,1] :='1'; 
     StringGrid1.Cells[0,2] :='2'; 
     StringGrid1.Cells[0,3] :='3'; 
     StringGrid1.Cells[0,4] :='4'; 
     StringGrid1.Cells[0,5] :='5'; 
     StringGrid1.Cells[0,6] :='6'; 
     StringGrid1.Cells[0,7] :='7'; 
     StringGrid1.Cells[0,8] :='8'; 
     StringGrid1.Cells[0,9] :='9'; 
     StringGrid1.Cells[0,10]:='10'; 
     { HARD-CODE A BUNCH OF NUMBERS. NOTE THAT HASH IN FRONT OF A NUMBER IS SIMPLY A FLAG FOR IsImportant } 
     StringGrid1.Cells[1,0] :='-10'; 
     StringGrid1.Cells[1,1] :='-6.86999999'; 
     StringGrid1.Cells[1,2] :='76.0999999'; 
     StringGrid1.Cells[1,3] :='#10.25';   
     StringGrid1.Cells[1,4] :='#17.2900006'; 
     StringGrid1.Cells[1,5] :='#57.1599993'; 
     StringGrid1.Cells[1,6] :='21.86000'; 
     StringGrid1.Cells[1,7] :='6.17'; 
     StringGrid1.Cells[1,8] :='27.219999'; 
     StringGrid1.Cells[1,9] :='#32.56000'; 
     StringGrid1.Cells[1,10]:='-1.7999'; 
    end; 


    Function TFinancialColumn.CreateCellControl : TStyledControl; 

    begin 
     Result:=TFinancialCell.Create(Self); 

     TTextCell(Result).OnTyping:=DoTextChanged; 
     TTextCell(Result).OnExit :=DoTextExit; 
    end; 

    Constructor TFinancialCell.Create(AOwner : TComponent); 

    begin 
     inherited; 
     StyleLookup:='textcellstyle'; 
     StyledSettings:=StyledSettings-[TStyledSetting.ssStyle,TStyledSetting.ssFontColor]; { THIS LINE MUST BE HERE TO APPLY A NEW STYLE; IT CLEARS THE 'DEFAULT' STYLE SETTINGS } 
     TextAlign:=TTextAlign.taTrailing; 
    end; 

    Procedure TFinancialCell.SetData(const Value   : TValue); 

    var 
     F             : Single; 
     O             : TFMXObject; 
     S             : String; 

    begin 
     S:=Value.AsString; 

     If Length(S)>1 then 
     begin 
     FIsImportant:=S[1]='#'; 
     If IsImportant then 
      S:=Copy(Value.AsString,2,MaxInt) 
     else 
      S:=Value.AsString; 

     F:=StrToFloat(S); 
     inherited SetData(Format('%n',[F])); 
     FIsNegative:=F<0; 

     ApplyStyling; 
     end; 
    end; 

    Procedure TFinancialCell.ApplyStyle; 

    var 
     T             : TFMXObject; 

    begin 
     inherited; 

     T:=FindStyleResource('rectangle1'); 

     If T is TRectangle then 
     begin 
     If IsNegative then 
     begin 
      TRectangle(T).Fill.Color:=claRed; 
     end; 
     end; 

     ApplyStyling; 
    end; 

    Procedure TFinancialCell.ApplyStyling; 

    var 
     T             : TFMXObject; 

    begin 
     If IsNegative then 
     FontColor:=claBlack 
     else 
     FontColor:=claGreen; 

     If IsImportant then Font.Style:=[TFontStyle.fsItalic,TFontStyle.fsBold]; { REPEAT THE ITALIC ELSE IT WILL ONLY BE BOLD, IE IT OVERWRITES THE ITALIC WITH BOLD } 

     If Assigned(Font.OnChanged) then 
     Font.OnChanged(Font); 

     Repaint; 
    end; 

이 코드는 글꼴과 배경을 다시 스타일 측면에서 작동합니다. 위의 내용을 개선하기위한 제안이 있으면 환영하지만 희망적으로 도움이 될 수 있습니다.

스크롤링을 시작하자마자 스타일이 떨어져 나갑니다. 아직 해결 방법을 찾지 못했습니다. 어떤 제안이라도 대환영!

관련 문제