wpf ListViewItem 自定义模板:ContentPresenter 保持为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14323231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:01:47  来源:igfitidea点击:

ListViewItem custom template: ContentPresenter stays empty

wpfxamltemplateslistviewitemcontentpresenter

提问by Modus Operandi

I have the following ListView in my code. views:GameCardis a custom UserControland {Binding}is a valid DataContextobject with three items. Without the custom ItemContainerStyleeverything works perfectly — the list shows three GameCardswith correct info, etc. As soon as I add the ItemContainerStylepart, I get nothing but three "ABCD"s; so the data is still loaded correctly, but my UserControlis no longer displayed (I only added the "ABCD" to check if the data was there, as otherwise I got nothing but empty box).

我的代码中有以下 ListView。views:GameCard是一个自定义对象UserControl并且{Binding}是一个DataContext包含三个项目的有效对象。如果没有自定义,ItemContainerStyle一切都可以完美运行——列表显示了三个GameCards具有正确信息的信息,等等。一旦我添加了ItemContainerStyle零件,我只得到三个“ABCD”;所以数据仍然正确加载,但我UserControl的不再显示(我只添加了“ABCD”来检查数据是否在那里,否则我只得到空框)。

Every piece of info I could find online seems to indicate that just putting a ContentPresenterelement in the template should work, but it doesn't seem to in this case. What am I missing?

我在网上能找到的每一条信息似乎都表明只ContentPresenter在模板中放置一个元素应该可以工作,但在这种情况下似乎没有。我错过了什么?

<ListView Grid.Row="1" ItemsSource="{Binding}" BorderThickness="0,0,1,0"
              ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListView.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FF614B4B" Offset="0"/>
            <GradientStop Color="#FFDA7070" Offset="1"/>
        </LinearGradientBrush>
    </ListView.Background>
  <ListView.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel />
    </ItemsPanelTemplate>
  </ListView.ItemsPanel>
  <ListView.ItemTemplate>
    <DataTemplate>
      <views:GameCard />
    </DataTemplate>
  </ListView.ItemTemplate>
  <ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate>
            <Grid>
              <TextBlock Text="ABCD" />
              <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
            </Grid>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </ListView.ItemContainerStyle>
</ListView>

回答by Clemens

You need to set the TargetTypeof your ControlTemplate. And in order to make your ItemTemplate work, you'd also need to bind the Contentand ContentTemplateproperties.

您需要设置ControlTemplate的TargetType。并且为了使您的 ItemTemplate 工作,您还需要绑定ContentContentTemplate属性。

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListViewItem">
                    <Grid>
                        ....
                        <ContentPresenter
                            Content="{TemplateBinding Content}"
                            ContentTemplate="{TemplateBinding ContentTemplate}"
                            ... />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ListView.ItemContainerStyle>

回答by Sten Petrov

This may not be the case with you but so far I haven't had to modify the ItemContainerStyle yet, just the ListView.View. Since you're putting a Grid in your template style I'd assume you're looking for a GridView and this is how you do that:

这可能不是你的情况,但到目前为止我还没有修改 ItemContainerStyle,只需要修改 ListView.View。由于您将 Grid 放在模板样式中,我假设您正在寻找 GridView ,这就是您这样做的方式:

<ListView.View>
    <GridView>
        <GridViewColumn Width="120">
            <GridViewColumnHeader Height="14" >
                <TextBlock Text="Type" FontSize="9"/>
            </GridViewColumnHeader>
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock  Text="{Binding Path=Name, FallbackValue=MISSING}" /> 
    <!-- or content presenter with bindings here -->
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
...

回答by yo chauhan

your style and everything is fine .All what required is to Assign or bind the Content propertyto your ContentPresenter. I hope this will help.

您的风格和一切都很好。所有需要的是将内容属性分配或绑定到您的 ContentPresenter。我希望这将有所帮助。