2011-06-11 9 views
6

내 상황은 다음과 같습니다. WPF에서 하나의 컨트롤에 두 가지 스타일을 결합하는 방법이 있습니까?

이 같은 ListView에 대한 스타일을 포함하는 App.xaml 있습니다 그러나

<Style x:Key="{x:Type ListViewItem}" TargetType="ListViewItem"> 
    <Setter Property="SnapsToDevicePixels" Value="true"/> 
    <Setter Property="OverridesDefaultStyle" Value="true"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListBoxItem"> 
       ... 

, 나는 다른 XAML에 어떤 스타일을 추가 싶어을의이 같은 Window.xaml에 가정 해 봅시다 :

<ListView AlternationCount="2" Background="#FFECECEC"> 
    <ListView.Resources> 
     <Style x:Key="{x:Type ListViewItem}" TargetType="{x:Type ListViewItem}"> 
      <EventSetter Event="PreviewMouseDoubleClick" Handler="OnPreviewMouseDoubleClick" /> 
     </Style> 
    </ListView.Resources> 
</ListView> 

그래서 App.xaml의 기본 디자인 스타일을 기본 스타일로 정의하고 싶습니다. 그런 다음 상황에 맞는 메뉴를 추가하고 각 xaml에서 이벤트를 추가하는 등의 수정 작업을 추가하십시오.

그러나 위 구현에서는 App.xaml에 정의 된 Style이 Window.xaml에 정의 된 Style에 의해 덮어 쓰기됩니다.

문제를 해결하고이를 달성 할 수있는 방법이 있습니까?

<Style x:Key="Style1"> 
... 
</Style> 

<Style x:Key="Style2" BasedOn="{StaticResource Style1}"> 
... 
</Style> 

Btw는 : <Style x:Key="{x:Type ListViewItem}"이 조금 이상한 것 같다

+0

태그 [coding-style] (http://stackoverflow.com/tags/coding-style/info)에 대한 설명을 읽고 적절한 조치를 취하시기 바랍니다. 감사! –

답변

9

스타일은 BasedOn 속성이 있습니다. x:Key은 xaml 사전의 고유 키 여야합니다. 대개 문자열입니다.

+0

빠른 답변 주셔서 감사합니다. 오, 그건 사실이야. x : 키가 이상해. 고마워. :) – Aki24x

관련 문제