使用celltemplate绑定要在gridviewcolumn中显示的集合

时间:2013-08-15 08:36:49

标签: c# wpf data-binding

我有一个项目集合,其中最后一列是一个集合,其中包含一个自己的celltemplate。 ListView的项源在代码中动态设置,所有其他列都正确呈现。但是,最后一列的数据根本没有被读取。我尝试过其他问题的不同解决方案,但它们似乎对我不起作用。

    <Window x:Class="bbowl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
        Title="Blood Bowl Data" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate x:Key="SkillTemplate">
            <TextBlock Text="{Binding Path=name}" ToolTip="{Binding Path=description}" Height="18" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="Blue"><TextBlock.TextDecorations><TextDecoration /></TextBlock.TextDecorations></TextBlock>
        </DataTemplate>
        <DataTemplate x:Key="SkillsTemplate">
            <DataGrid AutoGenerateColumns="False" Height="Auto" HorizontalAlignment="Left" VerticalAlignment="Top" HeadersVisibility="None" HorizontalScrollBarVisibility="Hidden" ItemsSource="{Binding Path=Skills}" BorderThickness="0" ItemTemplate="{StaticResource SkillTemplate}" />
        </DataTemplate>

    </Window.Resources>
    <Grid>
        <ComboBox HorizontalAlignment="Left" Name="cbxRace" VerticalAlignment="Top" Width="503" Height="25" SelectionChanged="cbxRace_SelectionChanged" ItemsSource="{Binding}" />
        <Label Content="Rerolls:" Height="25" HorizontalAlignment="Left" Margin="0,25,0,0" Name="lblRerollLabel" VerticalAlignment="Top" Width="435" HorizontalContentAlignment="Right" FontWeight="Bold" />
        <Label Content="0" Height="25" HorizontalAlignment="Left" Margin="441,25,0,0" Name="lblReroll" VerticalAlignment="Top" Width="62" HorizontalContentAlignment="Right" />
        <ListView Height="263" HorizontalAlignment="Left" Margin="0,48,0,0" Name="lvwPlayer" VerticalAlignment="Top" Width="503" ItemsSource="{Binding}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=name}"/>
                    <GridViewColumn Header="MA" DisplayMemberBinding="{Binding Path=ma}" />
                    <GridViewColumn Header="ST" DisplayMemberBinding="{Binding Path=st}" />
                    <GridViewColumn Header="AG" DisplayMemberBinding="{Binding Path=ag}" />
                    <GridViewColumn Header="Max" DisplayMemberBinding="{Binding Path=max}" />
                    <GridViewColumn Header="Price" DisplayMemberBinding="{Binding Path=price}" />
                    <GridViewColumn Header="Skills" CellTemplate="{StaticResource SkillsTemplate}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:1)

要解决您的问题,您需要将SkillsTemplate更改为以下内容:

<DataTemplate x:Key="SkillsTemplate">
   <DataGrid 
      AutoGenerateColumns="False" 
      HorizontalAlignment="Left" 
      VerticalAlignment="Top" 
      HeadersVisibility="None" 
      ItemsSource="{Binding Path=Skills}" 
      BorderThickness="0" 
      GridLinesVisibility="None">
      <DataGrid.Columns>
         <DataGridTemplateColumn CellTemplate="{StaticResource SkillTemplate}"/>
      </DataGrid.Columns>
   </DataGrid>
</DataTemplate>

而不是更改整个项目模板会创建一个使用SkillTemplate显示文本的列。但是,到目前为止,我可以看到,对于你想要用它做什么,你可以考虑使用不那么复杂的控件,如ListBox甚至ItemsControl,当你只想显示一堆项目而不给用户选项时选择一个

<DataTemplate x:Key="SkillsTemplate">
   <ItemsControl ItemsSource="{Binding Path=Skills}" ItemTemplate="{StaticResource SkillTemplate}"/>
</DataTemplate>