简体   繁体   English

祖先绑定仅在ListView中起作用一次

[英]Ancestor binding works only once in ListView

I have a Style in my Window.Resources: 我的Window.Styles中有一个样式

<Style x:Key="Header" TargetType="GridViewColumnHeader">
        <Setter Property="Content">
            <Setter.Value>
                <StackPanel Orientation="Horizontal">
                    <Label Width="80" HorizontalContentAlignment="Center" Content="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=GridViewColumnHeader, AncestorLevel=1}, Path=Tag}" />
                    <StackPanel Orientation="Vertical">
                        <Polygon Name="P_up" Points="0,5 10,5, 5,0" Stroke="Black" Fill="Black" Margin="3" Visibility="Visible"/>
                        <Polygon Name="P_down" Points="0,0 10,0, 5,5" Stroke="Black" Fill="Black" Margin="3" Visibility="Hidden"/>
                    </StackPanel>
                </StackPanel>
            </Setter.Value> 
        </Setter>
        <EventSetter Event="Click" Handler="Header_Click"/>
    </Style>

When I set this style to my ListView (GridViewColumnHeader) like this: 当我像这样将样式设置为ListView (GridViewColumnHeader)时:

<ListView Height="300" x:Name="lv" ItemsSource="{Binding PLCs}">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="120">
                    <GridViewColumnHeader Name="myNewText" Tag="test" Style="{StaticResource Header}"/>
                </GridViewColumn>
                <GridViewColumn Width="120">
                    <GridViewColumnHeader Name="myNewText2" Tag="test2"  Style="{StaticResource Header}"/>
                </GridViewColumn>
                <GridViewColumn Width="120">
                    <GridViewColumnHeader Tag="test32"  Style="{StaticResource Header}"/>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>        

The binding to the ancestors Tag works and the Polygon s are shown, but only for the last one of the GridViewColumns , the first two are staying blank. 与祖先Tag的绑定有效,并且显示了Polygon ,但是仅对于GridViewColumns的最后一个,前两个保持空白。 Can anyone tell me what I am doing wrong? 谁能告诉我我在做什么错? Since the code is the same for all three columns I assumed it would provide me withe the same results for all three. 由于所有三列的代码都相同,因此我认为这将为我提供全部三列相同的结果。 I guess either the Content binding of the Label or the TargetType of the Style is not correct. 我猜是LabelContent绑定或StyleTargetType不正确。

Thanks for your help. 谢谢你的帮助。

You should use the ContentTemplate property: 您应该使用ContentTemplate属性:

<Setter Property="ContentTemplate">
    <Setter.Value>
         <DataTemplate>
          <! -- ...place your stack panel here -->
         </DataTemplate>
    </Setter.Value>
</Setter>

Doing so, you define a template for each column header. 这样做,您可以为每个列标题定义一个模板 The template will instantiate the content for each column header independently. 该模板将独立实例化每个列标题的内容。

If you use the Content property, you set the same stack panel as the content to all the column headers. 如果使用Content属性,则将与内容相同的堆栈面板设置为所有列标题。 This won't work - only the last one will win. 这行不通-只有最后一个赢。 The stack panel's parent will be automatically set to the last header. 堆栈面板的父项将自动设置为最后一个标题。 It's like: 就像是:

header1.Content = your_stack_panel;
your_stack_panel.Parent = header1;

header2.Content = your_stack_panel;
your_stack_panel.Parent = header2;

header3.Content = your_stack_panel;
your_stack_panel.Parent = header3;

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

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