WPF DataTemplateColumn 访问 DataTemplate 并设置 ItemsSource
我知道我正在做的事情很奇怪,但我希望它能发挥作用。我感觉哪里出了问题。
我在我的资源中定义了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 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.