2013-07-05 2 views
1

작은 질문이 있습니다. ListBox에 2 개의 항목 만 포함되어 있습니다. 각 항목은 Fill 특성이 검정으로 설정된 Path 컨트롤입니다.ListBox 안에있는 Path 컨트롤의 채우기 속성을 변경하십시오.

이제 목록 상자에서 항목 중 하나를 선택하면이 채우기 특성의 색을 변경하려고합니다 ... Style을 사용해야한다고 생각합니다. 그러나 이렇게 할 때 스타일에는 ContentPresenter이 경로에 매핑되며이 ContentPresenter에는 속성이 없기 때문에 IsSelected 트리거를 통해 변경할 수 있습니다!

즉, 필 속성을 매핑하는 스타일을 계속 사용하려면 어떻게해야합니까? WPF 프로젝트의 Window

나의 현재 XAML 코드 :

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    x:Class="XAMLPathStyleProblem.MainWindow" 
    x:Name="Window" 
    Title="MainWindow" 
    Width="640" Height="480"> 
    <Window.Resources> 
     <Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}"> 
      <Setter Property="Background" Value="Transparent"/> 
      <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
      <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
      <Setter Property="Padding" Value="2,0,0,0"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
         <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"> 
          <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
         </Border> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsSelected" Value="true"> 
           <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 
           <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>        
          </Trigger> 
          <MultiTrigger> 
           <MultiTrigger.Conditions> 
            <Condition Property="IsSelected" Value="true"/> 
            <Condition Property="Selector.IsSelectionActive" Value="false"/> 
           </MultiTrigger.Conditions> 
           <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
           <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
          </MultiTrigger> 
          <Trigger Property="IsEnabled" Value="false"> 
           <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Window.Resources> 

    <Grid x:Name="LayoutRoot"> 
      <ListBox x:Name="ImageBar" ItemContainerStyle="{DynamicResource ListBoxItemStyle}"> 
      <ListBox.ItemsPanel> 
       <ItemsPanelTemplate> 
        <StackPanel Orientation="Horizontal" VerticalAlignment="Top" /> 
       </ItemsPanelTemplate> 
      </ListBox.ItemsPanel> 
      <ListBoxItem> 
       <Path Data="M15.992466,14.680105C20.892824,14.680104 23.97299,17.360288 28.013305,17.360288 31.943504,17.360288 34.333683,14.680104 39.994102,14.680105 44.274425,14.680104 48.804641,17.000391 52.034961,21.0308 41.454162,26.831151 43.174373,41.942682 53.865043,45.983101 52.394974,49.24342 51.694851,50.703518 49.794819,53.583672 47.154625,57.614079 43.424389,62.624561 38.803959,62.664604 34.703705,62.704647 33.643696,59.994431 28.073246,60.024464 22.50292,60.054249 21.342806,62.714657 17.23254,62.674614 12.622241,62.634571 9.0819604,58.104115 6.441766,54.083717 -0.95864094,42.822647 -1.7287129,29.611443 2.8315349,22.590761 6.0717456,17.600301 11.19209,14.680104 15.992466,14.680105z M38.751411,0C39.321331,3.8093758 37.761547,7.538764 35.701835,10.178331 33.502144,12.997869 29.702673,15.197509 26.033186,15.077528 25.373277,11.438125 27.093038,7.6887398 29.172746,5.1591539 31.462427,2.3696117 35.39188,0.23996067 38.751411,0z" 
         Fill="Black" /> 
      </ListBoxItem> 
      <ListBoxItem> 
       <Path Data="M32.127438,4.0459317E-05C34.679321,-0.0059787218,51.370113,0.63573532,60.553993,18.050023L60.522991,18.050023 60.543671,18.086075C61.200066,19.24132 68.004066,31.93957 59.575981,47.967091 59.575981,47.967091 51.176838,64.148377 30.558096,63.870453L29.65756,63.847004 29.649204,63.861397C29.644096,63.870198,29.641504,63.874661,29.641504,63.874661L29.638971,63.874695 29.681444,63.800747C30.804413,61.84562,39.865662,46.068413,42.345753,41.710415L42.378082,41.653572 42.392643,41.638874 42.472183,41.501145 42.692501,41.246766C44.087284,39.55642,45.09919,37.538369,45.595421,35.325478L45.613995,35.233231 45.681602,34.931549C45.857914,34.084336,45.977459,33.046578,45.939392,31.839535L45.927822,31.607016 45.927765,31.604247 45.927345,31.597495 45.913135,31.311926 45.901112,31.172703 45.89138,31.015126 45.867527,30.783802 45.865814,30.76396 45.8638,30.747662 45.831325,30.432713C45.783504,30.046782,45.720222,29.665644,45.64212,29.289938L45.605244,29.129017 45.579826,29.001641C45.3101,27.769034 44.871658,26.423209 44.200989,24.977549 43.870582,24.491171 43.539108,24.000555 43.182049,23.514327L42.899601,23.140976 60.287002,18.042616C60.287002,18.042616,39.292564,18.022913,34.351002,18.039915L34.393581,18.050023 34.172077,18.050023C33.849613,18.050023,33.54248,18.050023,33.252323,18.050023L33.158501,18.050023 32.880497,18.023783C32.497307,17.992794 32.109821,17.977 31.718649,17.977 31.350422,17.977 30.985464,17.990992 30.624279,18.018473L30.292705,18.050023 30.278829,18.050023C30.225145,18.050023 30.197481,18.050023 30.197481,18.050023 30.197481,18.050023 30.175093,18.049284 30.131918,18.049599 29.747402,18.052403 27.714258,18.138693 25.166611,19.573328L25.156681,19.579142 25.090729,19.612418C22.198151,21.138638,19.8955,23.632718,18.613605,26.663617L18.496868,26.959704 5.5749997,14.177C5.5749997,14.177,15.021078,30.765849,17.85829,35.692574L17.988001,35.917668 18.035503,36.093228C19.728666,42.05547 25.213291,46.422997 31.718649,46.422997 32.332252,46.422997 32.936783,46.384125 33.529907,46.308712L33.816097,46.268658 29.596954,63.874993 29.542429,63.874833C28.213777,63.865578 13.814976,63.407895 4.1510181,48.093563 4.1510176,48.093563 -5.6624084,32.728032 4.8882693,15.L5.3907794,14.192161 5.3934535,14.187385C5.6228327,13.780242 13.109029,0.74591898 31.796461,0.0057129142 31.796461,0.0057133239 31.911178,0.00055203911 32.127438,4.0459317E-05z" 
         Fill="Black" /> 
      </ListBoxItem> 
     </ListBox> 
    </Grid> 
</Window> 

답변

0

좋아 ... 직접 답변을 찾았습니다. 경로 컨트롤의 채우기 값을 바인딩 컨트롤로 변경하면 모든 항목이 예상대로 작동합니다! RelativeSource ListBoxItem의에 바인딩

Fill="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" 
또한
1

내가 MVVM 라이트 툴킷와 MVVM 패턴을 사용을 권장합니다. 그런 다음 목록에 표시 할 데이터 컬렉션을 만들어야합니다. 귀하의 경우에는 데이터 및 채우기 (색)가 될 수 있습니다. 그런 다음 Click 이벤트를 처리해야합니다. 이러한 기능을 제공하는 코드가 있으므로 원하는 경우 물어보십시오.

<Style x:Key="Path" TargetType="{x:Type Path}"> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding RelativeSource={RelativeSource, AncestorType={x:Type ListBoxItem}}, Path=IsSelected" Value="True"> 
      <Setter Property="Fill" Value="YouColor"/> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding RelativeSource={RelativeSource, AncestorType={x:Type ListBoxItem}}, Path=IsSelected" Value="False"> 
      <Setter Property="Fill" Value="YouAnotherColor"/> 
      </DataTrigger> 
    </Style.Triggers> 
</Style> 

감사합니다 :

그러나 (MVVM 라이트 툴킷없이) 귀하의 경우 오히려 같은 STH 할 것에

. 마테우스

+0

좋은 제안하지만, 사용은 – Depechie

+0

네는 확실히 :)하지만 지금까지 내가 아는 한, 당신이 먼저 ListBoxItem의의 전경 색상을 변경 작동하고이 경로의 변화 색상 채우기 제공 변경합니다. 나는 그것이 한 곳에서 이루어질 수 있다고 생각한다. –

+0

네, 그게 사실입니다 :)하지만 내 질문에 스타일을 정의 했더라도 ... 내 솔루션 (contentpresenter를 지적했다)으로 더 이상 필요하지 않고 단지 목록 상자의 기본 동작을 사용합니다. – Depechie

관련 문제