【问题标题】:How to set a specific Datacontext for a DataTemplate in CellTemplate如何在 CellTemplate 中为 DataTemplate 设置特定的 Datacontext
【发布时间】:2013-12-04 07:56:07
【问题描述】:

目前我绑定到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]} 是问题部分,因为它不存在......

【问题讨论】:

  • 我不明白你的问题......为什么你需要设置DataGridTemplateColumnDataContext?这已经由DataGrid 控件隐式完成,不是吗?在DataTemplate 中,您应该已经可以访问集合中对象的属性,该集合中的数据绑定到DataGrid.ItemsSource 属性。
  • 我的 DataContext 是一个访问对象的列表,我需要指定列表中的项目,例如 [0] (获取第一个),但如果我使用 DataTemplate作为 StaticResource 我需要指定 DataContext ...
  • 这个List&lt;T&gt; 不是设置为DataGrid.ItemsSource属性吗? 如果我使用 DataTemplate 作为 StaticResource,我需要指定 DataContext - 我认为这不是真的。唯一需要指定DataContext 的时间(在这种情况下)是,如果您想将Bind 指向与集合中数据绑定到DataGrid.ItemsSource 属性的对象不同的对象... 你想做什么?
  • 不,更复杂的是我的 DataGrid.ItemsSource 是一个 List ,其中包含 2 个属性,第一个是 MyRowheader,第二个是 MyCellList,这是一个 List&lt;T&gt;跨度>
  • 您只想在DataGrid 的每一行中显示来自MyCellList 的一个值?如果正确,它是 same 值,还是每行与MyCellList 不同的值?

标签: wpf binding datagrid datatemplate celltemplate


【解决方案1】:

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

<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 页面上发现重复的代码。

我能想到的唯一可以编写更少代码的方法是从代码动态生成列。您可以在Dynamically add Columns to DataGrid in wpf 帖子中找到一个基本示例。我不知道这会为你节省多少时间。

【讨论】:

  • 好的,现在你得到了我想要实现的目标,但我想删除重复,因为基本上它总是相同的代码。如果您仔细查看 XAML,您会看到您的答案是我已经在做的 (ObColl[1] == MyCellListl[1])。恐怕我的英语不够好,无法更好地描述问题
  • 我从您的示例中获取了该代码,所以是的,该位是相同的。我仍在努力完全理解你想要什么。你能解释一下'删除重复'是什么意思吗?
  • 好的,让我再试一次,Datatemplate 总是一样的,唯一会改变的是我的 ObColl[??] 中的索引,因为 DataContext 是 MyRow 对象,所以我尝试指定 @ 987654333@ 获取MyCell 作为DataContextDatatemplate
  • 哦...当我问你每行与MyCellList的值是相同还是不同时,你说相同的值...您现在是说每一行都应该显示与MyCellList 集合不同的值吗?
  • Noo ...每一列都需要新索引而不是行(我将编辑我的问题)
猜你喜欢
  • 2016-02-06
  • 2011-01-26
  • 1970-01-01
  • 1970-01-01
  • 2015-04-15
  • 2016-04-30
  • 1970-01-01
  • 2014-08-26
  • 2016-09-24
相关资源
最近更新 更多