将自定义模板添加到ListView - GridView WPF?

时间:2011-07-05 00:53:03

标签: c# wpf listview gridview celltemplate

我目前有这个XAML代码:

<ListView x:Name="listFeedSearch" Margin="-548.856,95.333,0,7.667" HorizontalAlignment="Left" Width="542.5" RenderTransformOrigin="0.5,0.5" ItemsSource="{Binding SearchCollection}">
    <ListView.RenderTransform>
        <TransformGroup>
            <ScaleTransform/>
            <SkewTransform/>
            <RotateTransform/>
            <TranslateTransform/>
        </TransformGroup>
    </ListView.RenderTransform>
    <ListView.View>
        <GridView>
            <GridViewColumn Width="150" Header="Feed Name" x:Name="listFeedSearchName" DisplayMemberBinding="{Binding FeedName}"  />
            <GridViewColumn Width="280" Header="Feed Address" x:Name="listFeedSearchAddress" DisplayMemberBinding="{Binding FeedUrl}" />
            <GridViewColumn Width="100" Header=" " x:Name="listFeedSearchSelect" />
        </GridView>
    </ListView.View>
</ListView>

我想要做的是让listFeedSearchSelect在每个记录的单元格中都有一个Button。

我曾试图做的是,但没有奏效,它只是显示为空格:

<GridViewColumn Header=" " Width="100" x:Name="listFeedSearchSelect">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
                <Button Content="Select" />
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

任何人都可以帮助我,谢谢。

2 个答案:

答案 0 :(得分:2)

尝试将模板设置为资源。像这样:

<Window.Resources>
    <DataTemplate x:Key="SelectButtonColumnDataTemplate">
        <Button Content="Select" Command="{Binding SelectItemCommand}" />
    </DataTemplate>
</Window.Resources>
<ListView x:Name="listFeedSearch" HorizontalAlignment="Left" Width="542.5" RenderTransformOrigin="0.5,0.5" ItemsSource="{Binding SearchCollection}">
    <ListView.RenderTransform>
        <TransformGroup>
            <ScaleTransform/>
            <SkewTransform/>                
            <RotateTransform/>                
            <TranslateTransform/>            
            </TransformGroup>        
            </ListView.RenderTransform>        
    <ListView.View>            
        <GridView>                
            <GridViewColumn Width="150" Header="Feed Name" x:Name="listFeedSearchName" DisplayMemberBinding="{Binding FeedName}"  />                
            <GridViewColumn Width="280" Header="Feed Address" x:Name="listFeedSearchAddress" DisplayMemberBinding="{Binding FeedUrl}" />                
            <GridViewColumn Width="100" Header=" " CellTemplate="{StaticResource SelectButtonColumnDataTemplate}" />            
        </GridView>        
        </ListView.View>    
</ListView>

答案 1 :(得分:0)

如果我只是复制并粘贴你的GridViewColumn集合中有项目,它确实会按预期显示一个按钮。问题可能是你的背景。