简体   繁体   English

使用celltemplate绑定集合以显示在gridview列中

[英]Binding a collection for display in a gridviewcolumn using celltemplate

I have a collection of items where the last column is a collection with a celltemplate of it's own. 我有一个项目的集合,其中最后一列是具有其自己的celltemplate的集合。 The ListView's item source is set dynamically in code and all of the other columns are rendering correctly. ListView的项目源是在代码中动态设置的,其他所有列均正确呈现。 However, the final column's data is not being read at all. 但是,根本不会读取最后一列的数据。 I've tried different solutions from other questions, but they don't seem to work for me. 我尝试了与其他问题不同的解决方案,但它们似乎对我没有用。

    <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>

To solve your problem you would need to change your SkillsTemplate to something like this: 为了解决您的问题,您需要将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>

which instead of changing whole items template creates one column that displays your text using SkillTemplate . 而不是更改整个项目模板,而是创建一列,以使用SkillTemplate显示您的文本。 However, as far I can see, for what you want to do with it you may consider using less complicated control like ListBox or even ItemsControl when you just want to display bunch of items without giving user option to select one 但是,据我所知,对于您要使用的ListBox ,当您只想显示一堆项目而不给用户选择选项时,可以考虑使用不太复杂的控件(如ListBoxItemsControl

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

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

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