【问题标题】:WPF : Get column header from a specific cellWPF:从特定单元格获取列标题
【发布时间】:2015-11-16 21:09:40
【问题描述】:

我有一个数据网格,我在其中根据几个标准(日期和部门)动态创建列。每列实际上是一个列表框,其中包含要拖动的项目。

我的问题是每个列表框项目的来源都不同。如果通过阅读标题,我可以看到如何找到源的唯一方法(我想向我的列和单元格添加某种参数,但我找不到它。

有什么办法吗?

问候,

【问题讨论】:

  • 你应该展示你目前使用的代码来帮助人们帮助你

标签: wpf binding datagrid


【解决方案1】:

您可以将您的 xaml 设置为

<Window.Resources>
    <DataTemplate x:Key="Field1CellTemplate" >
    </DataTemplate>
    <DataTemplate x:Key="DeptCellTemplate" >
    </DataTemplate>
</Window.Resources>
<Grid>
    <DataGrid Name="grid" AutoGenerateColumns="false">
         <DataGrid.Columns>
            <DataGridTemplateColumn  Header="Field1" CellEditingTemplate="{StaticResource Field1CellTemplate}" Width="100">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Field1}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn >
            <DataGridTemplateColumn  Header="Dept" CellEditingTemplate="{StaticResource DeptCellTemplate}" Width="100">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Dept}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn >

        </DataGrid.Columns>

    </DataGrid>
</Grid>

并且您已将每列数据的 itemssource 动态设置为

DataTemplate Field1CellTemplate = this.TryFindResource("Field1CellTemplate") as DataTemplate;
        if (Field1CellTemplate != null)
        {
            var frameworkElementFactory = new FrameworkElementFactory(typeof(ComboBox));
            frameworkElementFactory.SetValue(ComboBox.ItemsSourceProperty, new object[] { "option1", "option2", "option3" });
            Field1CellTemplate.VisualTree = frameworkElementFactory;
        }

        DataTemplate DeptCellTemplate = this.TryFindResource("DeptCellTemplate") as DataTemplate;
        if (Field1CellTemplate != null)
        {
            var frameworkElementFactory = new FrameworkElementFactory(typeof(ComboBox));
            frameworkElementFactory.SetValue(ComboBox.ItemsSourceProperty, new object[] { "CS", "ETC", "IT" });
            DeptCellTemplate.VisualTree = frameworkElementFactory;
        }

        DataTable dt = new DataTable();
        dt.Columns.Add("Field1");
        dt.Columns.Add("Dept");
        dt.Rows.Add(new object[] { "option1", "CS"});
        dt.Rows.Add(new object[] { "option3", "IT" });
        dt.Rows.Add(new object[] { "option2", "ETC" });

        grid.ItemsSource = dt.DefaultView;

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    原来它很简单

     Dim oLstBox As ListBox = e.Source
     Dim poste As T_Ref_Poste = oLstBox.DataContext
     Dim row = dgHoraire.ItemContainerGenerator.ContainerFromItem(poste)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-02
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多