WPF DataTemplateColumn 访问 DataTemplate 并设置 ItemsSource

发布于 2024-09-27 06:43:01 字数 2323 浏览 7 评论 0原文

我知道我正在做的事情很奇怪,但我希望它能发挥作用。我感觉哪里出了问题。

我在我的资源中定义了一个 DataTemplate,如下所示:

  <UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../ParameterEditorResourceDictionary.xaml"></ResourceDictionary>
            <ResourceDictionary>

                <DataTemplate x:Key="ParameterDefault">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="("></TextBlock>
                        <ItemsControl ItemsSource="{//I need to set from code}">
                            //some code here
                        </ItemsControl>
                        <TextBlock Text=")"></TextBlock>
                    </StackPanel>
                </DataTemplate>

          </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>       
</UserControl.Resources>

我在我的 xaml 中定义了一个 DataGrid,它有一个已加载事件。

 <cc:PEDataGrid AutoGenerateColumns="False"
               Loaded="CommonPEGrid_Loaded">        
</cc:PEDataGrid>

在我的事件处理程序代码中,我想设置在我的 DataTemplate 中定义的 ItemsControl 的 ItemsSource。我的代码如下所示:

private void CommonPEGrid_Loaded(object sender, RoutedEventArgs e)
    {
        int i = 0;
        DataGrid dg = sender as DataGrid;

        DataGridTemplateColumn column = null;

        //ParametersAllLoops is a ObservableCollection

        foreach (ParameterLoop obj in ParametersAllLoops)
        {
            column = new DataGridTemplateColumn();
            column.Header = "Loop ( " + i.ToString() + " )";

            DataTemplate dt = null;

            //Here I want to write code
            //I want to access the DataTemplate defined in resources 
            //and set the ItemsSource of ItemsControl to something like this
            // xxx.ItemsSource = obj; and then assign the DataTemplate to 
            //the CellTemplate of column.
            //**Note :: ParameterLoop object has the IList Parameters**


            column.CellTemplate = dt;

            dg.Columns.Add(column);
            i++;            
        }
}

I know its strange what I am doing but I want this to work. I am going wrong somehwere I feel.

I have a DataTemplate defined in my resources as follows :

  <UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../ParameterEditorResourceDictionary.xaml"></ResourceDictionary>
            <ResourceDictionary>

                <DataTemplate x:Key="ParameterDefault">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="("></TextBlock>
                        <ItemsControl ItemsSource="{//I need to set from code}">
                            //some code here
                        </ItemsControl>
                        <TextBlock Text=")"></TextBlock>
                    </StackPanel>
                </DataTemplate>

          </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>       
</UserControl.Resources>

I have a DataGrid defined in my xaml which has a loaded event.

 <cc:PEDataGrid AutoGenerateColumns="False"
               Loaded="CommonPEGrid_Loaded">        
</cc:PEDataGrid>

In my event handler code I want to set the ItemsSource of ItemsControl defined in my DataTemplate. My code behind looks like this :

private void CommonPEGrid_Loaded(object sender, RoutedEventArgs e)
    {
        int i = 0;
        DataGrid dg = sender as DataGrid;

        DataGridTemplateColumn column = null;

        //ParametersAllLoops is a ObservableCollection

        foreach (ParameterLoop obj in ParametersAllLoops)
        {
            column = new DataGridTemplateColumn();
            column.Header = "Loop ( " + i.ToString() + " )";

            DataTemplate dt = null;

            //Here I want to write code
            //I want to access the DataTemplate defined in resources 
            //and set the ItemsSource of ItemsControl to something like this
            // xxx.ItemsSource = obj; and then assign the DataTemplate to 
            //the CellTemplate of column.
            //**Note :: ParameterLoop object has the IList Parameters**


            column.CellTemplate = dt;

            dg.Columns.Add(column);
            i++;            
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰ゝ天使的微笑 2024-10-04 06:43:01

您可以使用 FindResource() 方法查找资源并将其转换为 DataTemplate,但要为其分配 ItemSource,您将需要进行字符串操作。

看来您希望在数据网格上有动态列,我建议您在后面的代码中生成数据模板,以便您可以在那里解析绑定路径和源名称,然后将其附加为单元格模板或单元格编辑模板。

You can find the resource by using method FindResource() and cast it to DataTemplate but to assign it ItemSource you will need string manipulation.

It seems you want to have dynamic columns on your datagrid, I would suggest that you have that generate the datatemplate in code behind so that you can resolve your binding paths and source names there and then attach it as cell template or cell edit template.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文