상세 컨텐츠

본문 제목

지역 리소스와 전역리소스 예제

WPF

by happynuri 2008. 5. 20. 20:13

본문

How to change listbox, combobox or listview selection and background color

This is, probably, one of most common questions about styling in WPF - how to change selected item background color in ListBox (ComboBox, ListView etc). The answer is really simple - look into default control templates of those controls. Those templates using system brushed in order to paint background colors. So, if you want to customize an appearance of those controls, you always can rewrite templates, but why to do it if the only thing you want is to change color brush of elements, so KISS [keep it stupid simple] rewrite those system brushed like this:

      <ComboBox Background="Red">

        <ComboBox.Resources>

          <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow" />

          <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Green" />

          <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="Red" />

        </ComboBox.Resources>

        <TextBlock>TEST</TextBlock>

        <TextBlock>TEST</TextBlock>

        <TextBlock>TEST</TextBlock>

        <TextBlock>TEST</TextBlock>

        <TextBlock>TEST</TextBlock>

        <TextBlock>TEST</TextBlock>

        <TextBlock>TEST</TextBlock>

      </ComboBox>

 

 

[결과화면]

사용자 삽입 이미지

  덧붙여서 하이라이트 될때의 글자색은...

<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />

 

  - 지역 리소스가 전역리소스(또는 상위리소스) 보다 우선하여 적용된다는 것을 이용한 코드다...

    참... 모르면 어렵지만 이렇게 간단하게 된다니...  볼수록 wpf 팀, 설계 잘 한것 같어....

관련글 더보기

댓글 영역