如何在CellTemplate中为DataTemplate设置特定的DataContext?

4

目前我绑定了一个List<T>,所以我必须为每一列分别做特定的设置,并创建单独的DataTemplate

就像这样:

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
       <TextBlock TextAlignment="Center" 
                  Text="{Binding ObColl[1].Std, UpdateSourceTrigger=PropertyChanged}"
                  Background="{Binding ObColl[1].DienstColor, TargetNullValue=Transparent,FallbackValue=Transparent}" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

但我希望将DataTemplate作为资源创建一次

<DataGrid.Resources>
        <DataTemplate x:Name="MyCellTemplate">
           <TextBlock TextAlignment="Center" 
                      Text="{Binding Std, UpdateSourceTrigger=PropertyChanged}"
                      Background="{Binding DienstColor, TargetNullValue=Transparent,FallbackValue=Transparent}" />
        </DataTemplate>
</DataGrid.Resources>

并像这样使用它

<DataGridTemplateColumn CellTemplate="{StaticResource MyCellTemplate} ??{Binding ObColl[1]}??"/>

但是为了这样做,我需要在我的DataGridTemplateColumn中指定DataContext(ObColl[Idx]),但我该怎么做呢?


编辑

XAML应该像这样:

    <DataGrid Name="dataGrid1"
              ItemsSource="{Binding Itemlist, UpdateSourceTrigger=PropertyChanged}">
        <DataGrid.Resources>
            <DataTemplate x:Key="MyCellTemplate">
                <TextBlock TextAlignment="Center" 
                           Text="{Binding Std, UpdateSourceTrigger=PropertyChanged}" 
                            Background="{Binding DienstColor, TargetNullValue=Transparent, FallbackValue=Transparent}" />
            </DataTemplate>

        </DataGrid.Resources>

        <DataGrid.Columns> 
            <!-- Column 1 -->
            <DataGridTemplateColumn CellTemplate="{StaticResource MyCellTemplate}" 
                                    DataContext={Binding ObColl[0]}/> 
            <!-- Column Header 2 -->
            <DataGridTemplateColumn CellTemplate="{StaticResource MyCellTemplate}" 
                                    DataContext={Binding ObColl[1]}/> 
        </DataGrid.Columns>
    </DataGrid>

DataContext={Binding ObColl[1]} 是问题所在,因为它并不存在……


1
我不明白你的问题...为什么需要设置DataGridTemplateColumnDataContext?这已经被DataGrid控件隐式完成了,不是吗?在DataTemplate中,您应该已经可以访问绑定到DataGrid.ItemsSource属性的集合中对象的属性。 - Sheridan
我的DataContext是一个LIST,要访问对象,我需要指定列表中的项,例如[0](获取第一个)。但如果我使用DataTemplate作为StaticResource,我需要指定DataContext... - WiiMaxx
这个 List<T> 没有被设置为 DataGrid.ItemsSource 属性吗?如果我使用一个作为 StaticResource 的 DataTemplate,我需要指定 DataContext - 我不认为这是正确的。在这种情况下,唯一需要指定 DataContext 的时候是,如果你想要绑定到与数据绑定到 DataGrid.ItemsSource 属性的集合中的不同对象...那么这就是你想要做的事情吗? - Sheridan
不是很简单,我的 DataGrid.ItemsSource 是一个包含两个属性的 List<MyRow>,第一个属性是 MyRowheader,第二个属性是 MyCellList,它是一个 List<T> - WiiMaxx
你只想在DataGrid的每一行中显示MyCellList中的一个值吗?如果是这样,那么这个值是相同的还是每一行都展示来自MyCellList不同的值? - Sheridan
每行的值都是相同的。 - WiiMaxx
1个回答

1

好的,这是我对您要求的理解...您有一个名为MyRow的类,其中包含两个属性:MyRowheaderMyCellList。您希望在您的DataGrid的每一行上显示MyRowheader的值和MyCellList集合中的一个值。这是我会这样做:

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding YourCollection}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Header" Binding="{Binding MyRowheader, 
UpdateSourceTrigger=PropertyChanged}" />
        <DataGridTemplateColumn Header="Cell list">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding MyCellList[1].Std, UpdateSourceTrigger=
PropertyChanged}" Background="{Binding MyCellListl[1].DienstColor, TargetNullValue=
Transparent, FallbackValue=Transparent}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

请告诉我是否理解您的要求有误。

更新 >>>

所以我误解了您的要求。看起来您想在DataGrid的每个中从MyCellList集合中获取一个值,而不是行。在这种情况下,我的答案是否定的,您不能使用DataTemplate或任何其他XAML保存功能设置DataGrid.Columns。 XAML是一种冗长的语言...有一些编写更有效的方法,但并不多。您经常会在XAML页面上找到重复的代码。

我唯一能想到的可以减少代码量的方法是通过代码动态生成列。您可以在wpf中动态添加列到DataGrid帖子中找到基本示例。但我不知道那样能为您节省多少时间。


好的,现在你明白我想要实现什么了,但是我想要消除重复,因为基本上它总是相同的代码。如果你仔细看XAML,你会发现你的答案就是我已经做的(ObColl[1] == MyCellListl[1])。很抱歉,我的英语不够好,无法更好地描述问题。 - WiiMaxx
我从你的示例中拿了那段代码,所以是的,那一部分是相同的。我仍然在努力完全理解你想要什么。你能解释一下你所说的“去除重复”是什么意思吗? - Sheridan
好的,让我再试一次。DataTemplate始终是相同的,唯一会改变的是ObColl[??]中的索引,因为DataContext是MyRow对象,所以我尝试指定DataContext来获取MyCell作为Datatemplate的DataContext。 - WiiMaxx
哦...当我问你“每一行的值是来自于MyCellList的相同值还是不同值”时,你说“是相同的值”...现在你是在说每一行应该显示来自于MyCellList集合的不同值吗? - Sheridan
不是每一行,而是每一列都需要新的索引(我会修改我的问题) - WiiMaxx
请查看我的编辑。 - WiiMaxx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接