2014-05-19 1 views
0

ListView에서 상속 한 컨트롤과 ListViewItem에서 상속하는 컨트롤을 사용자 지정 컨트롤로 만들려고합니다. 외형은 괜찮습니다. 문제는 아래 표시된 컨트롤의 일부를 클릭 할 때 항목이 선택되지 않는다는 것입니다. 이미지에서 직접 클릭해야합니다. 텍스트에는 여백과 동일한 문제가 있었지만 (마진을 클릭 할 수는 없었 음) 텍스트에 여백을 추가했습니다. ContentPresenter.Content의 내용을 추가하면 문제가 해결 될 것이라고 생각했지만 그렇지 않았습니다. 차라리 generic.xaml을보다 내 자원에이 스타일을 추가하고, NavigationListViewItem.csDefaultStyleKeyProperty.OverrideMetadata 방법을 제거하는 경우ListViewItem 사용자 지정 컨트롤이 여전히 항목의 일부인 빈 공간에있는 클릭에 응답하지 않습니다.

enter image description here

또한, 올바르게 (가 무슨 일이야 확실하지 않은) 작업 않습니다.

사용자 지정 컨트롤의 코드 숨김에는 추가 코드가 추가되어 있지 않습니다.

<Style TargetType="{x:Type ui:NavigationListViewItem}"> 
    <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="Foreground" Value="White" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ui:NavigationListViewItem}"> 
       <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"> 
        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"> 
         <ContentPresenter.Content> 
          <StackPanel x:Name="itemPanel" 
              MaxWidth="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Path=ActualWidth}" 
              VerticalAlignment="Center"> 
           <Grid> 
            <Grid.ColumnDefinitions> 
             <ColumnDefinition Width="19" /> 
             <ColumnDefinition Width="*"/> 
            </Grid.ColumnDefinitions> 
            <Grid.RowDefinitions> 
             <RowDefinition MinHeight="22" /> 
            </Grid.RowDefinitions> 

            <ui:ColorableImage Grid.Column="0" Grid.Row="0" Source="arrow-right-white.png" Height="15" Width="15" /> 

            <TextBlock Grid.Column="1" Grid.Row="0" 
             x:Name="itemText" 
             FontSize="14" 
             FontFamily="Segoe UI" 
             FontWeight="Bold" 
             TextWrapping="Wrap" 
             VerticalAlignment="Center" 
             HorizontalAlignment="Stretch" 
             Text="Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text " 
             Padding="3,10,10,10" /> 
           </Grid> 
          </StackPanel> 
         </ContentPresenter.Content> 
        </ContentPresenter> 
       </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}}"/> 
         <Setter TargetName="itemText" Property="Foreground" Value="Red" /> 
        </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.InactiveSelectionHighlightBrushKey}}"/> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}"/> 
        </MultiTrigger> 
        <Trigger Property="IsEnabled" Value="false"> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

답변

1

NavigationListViewItem에 대한 귀하의 Border 배경은 기본적으로 null로 설정하고, 그 마우스 클릭에 반응하지 않습니다 여기에 generic.xaml을의 스타일입니다. 당신은 예를 TransparentTargetName을 지정할 필요없는 Background 변경하여 Trigger에도

<Style TargetType="{x:Type ui:NavigationListViewItem}"> 
    <!-- other setters --> 
    <Setter Property="Foreground" Value="White" /> 
    <Setter Property="Background" Value="Transparent" /> 

를 들어, 뭔가를 초기화해야합니다. 이미 TemplateBinding을 수행 했으므로 Background을 해당 항목에서 직접 변경해야합니다. 테두리의 배경 국경에 관계없이이 경우는 null인지 아닌지

<Trigger Property="IsSelected" Value="true"> 
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 
    <!-- other setters --> 
</Trigger> 
+0

그것은 항상 간단한 뭔가 ... 또한 은 왜 중요합니까? 테두리가 배경인지 여부는 사용자가 클릭 할 수 있는지 여부를 결정하는 것처럼 보이지 않습니다. –

+1

'Background' 브러시가 초기화되지 않은 경우 테스트가 표시되지 않습니다. 그것이 어떤 경우에 유용 할 수있는 방법 일뿐입니다. 컨트롤의 전체 영역에서 히트 테스트를 볼 수있게하려면'Background'를 무언가로 초기화해야합니다. – dkozl

+0

알았어, 그건 네가 알아야 할 것 같아. Background가 기본적으로 Transparent로 초기화되지 않은 이유가 궁금합니다. 편집 : 실제로, 신경 쓰지 말고, 코드 비하인드가 초기 화되었는지 아닌지를 확인하는 것은 의미가있다 ... XAML 목적을 위해 성가신 일이다. –

관련 문제