简体   繁体   English

重写ListViewItem会缩小列表视图中的所有项目

[英]Overriding ListViewItem shrinks all my items in the listview

So I have a ListViewItem style that I want to apply on the items (I just want to disable the disabled color Trigger on the listboxitems because it is ugly until/when I find a new color that I like better. 所以我有一个ListViewItem样式,我想在项目上应用(我只想在listboxitems上禁用禁用的颜色触发器,因为直到/当我找到我更喜欢的新颜色时,它很难看。

              <Setter Property="Template">
                  <Setter.Value>
                    <ControlTemplate TargetType="ListViewItem">
                      <Border 
                        Name="Border"
                        Padding="2"
                        SnapsToDevicePixels="true"
                        Background="Transparent"
                        >
                        <GridViewRowPresenter
                          Content="{TemplateBinding Content}"
                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                          />
                      </Border>
                      <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                          <Setter TargetName="Border"
                                  Property="Background" Value="{x:Static SystemColors.HighlightBrush}"
                                  />
                        </Trigger>
                      </ControlTemplate.Triggers>
                    </ControlTemplate>
                  </Setter.Value>
                </Setter>

However, upon adding this template to the listview, the items all shrink. 但是,将此模板添加到列表视图后,所有项目都会缩小。 Originally they were getting their item size from this binding (I think): Height={Binding ElementName=graph_viewer, Path=GraphHeight, Mode=OneWay}. 最初,他们是从此绑定中获取项目大小的(我认为):Height = {Binding ElementName = graph_viewer,Path = GraphHeight,Mode = OneWay}。 However, I don't see any reason why adding an itemcontainer style would break this binding unless gridviewpresenter is doing something strange. 但是,我看不出为什么添加itemcontainer样式会破坏此绑定,除非gridviewpresenter做一些奇怪的事情。

       <HierarchicalDataTemplate 
            ItemsSource ="{Binding Path = bits}"
            DataType="{x:Type ViewModels:BusViewModel}"
            >

              <Components:CenteredTextBlock
                x:Name="CommentTextBlock"
                BorderBrush="{Binding RelativeSource ={RelativeSource AncestorType={x:Type ListView}}, Path=BorderBrush}"
                HorizontalAlignment="Stretch"
                Height="{Binding ElementName=graph_viewer, Path=GraphHeight, Mode=OneWay}"
                >
                <Components:CenteredTextBlock.MainText>
                  <MultiBinding Converter="{StaticResource StringConcatConverter}">
                    <Binding Path="Alias" />
                    <Binding Path="SignalValueAtPrimaryMarker" />
                  </MultiBinding>
                </Components:CenteredTextBlock.MainText>
              </Components:CenteredTextBlock>
            </HierarchicalDataTemplate>

CenteredTexTBlock is just a centered text block (wanted to save having to type out grid and write in alignment = center repeatedly). CenteredTexTBlock只是一个居中的文本块(希望省去键入网格并重复写入alignment = center的麻烦)。

<Grid>
  <TextBlock 
    VerticalAlignment="Center"
    HorizontalAlignment="Center"
    Foreground="{Binding RelativeSource = {RelativeSource FindAncestor, AncestorType={x:Type Components:CenteredTextBlock}}, Path=Foreground}" 
    Text="{Binding RelativeSource = {RelativeSource FindAncestor, AncestorType={x:Type Components:CenteredTextBlock}}, Path=MainText}" 
    />
</Grid>

Default template of ListViewItem has ContentPresenter and not GridViewRowPresenter . ListViewItem默认模板具有ContentPresenter而不是GridViewRowPresenter

In case you only want to disable selection Trigger and want your's brush over there, so i would suggest to have default ControlTemplate only with your set of Triggers in it. 万一您只想禁用选择触发器并希望在那里刷一下,那么我建议仅在其中包含一组触发器的情况下使用默认ControlTemplate。

Default template goes like this with your triggers: 默认模板与您的触发器类似:

<ControlTemplate
    TargetType="ListViewItem">
    <Border
        BorderThickness="{TemplateBinding Border.BorderThickness}"
        Padding="{TemplateBinding Control.Padding}"
        BorderBrush="{TemplateBinding Border.BorderBrush}"
        Background="{TemplateBinding Panel.Background}"
        Name="Bd"
        SnapsToDevicePixels="True">
        <ContentPresenter
           Content="{TemplateBinding ContentControl.Content}"
           ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
           ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
           HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
           VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
           SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"/>
    </Border>
    <ControlTemplate.Triggers>
       <Trigger Property="IsSelected" Value="true">
           <Setter TargetName="Bd"
                   Property="Background"
                   Value="{x:Static SystemColors.HighlightBrush}"/>
       </Trigger>
     </ControlTemplate.Triggers>
 </ControlTemplate>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM