问题描述
我正在动态创建DataGrid列(基于ViewModel中的一个事件),并以编程方式将它们添加到现有的DataGrid中。每列使用通用的HeaderTemplate,将其设置为已在xaml中标识的DataTemplate。 DataTemplate包含两个标签,在创建DataGrid列时需要更改其内容。这将如何做?据了解,DataTemplate使用ContentPresenter,但我无法在动态创建的DataGrid列中访问它。代码如下:
xaml:(用于格式化DataGrid列标题的模板):
< DataTemplate x:Key =columnTemplate>
< StackPanel>
< Label Padding =0Name =labelA/>
<分隔符HorizontalAlignment =拉伸/>
< Label Padding =0Name =labelB/>
< / StackPanel>
< / DataTemplate>
c#(用于动态创建DataGrid列并将其添加到现有DataGrid中)
var dataTemplate = FindResource(columnTemplate)作为DataTemplate;
var column = new DataGridTextColumn();
column.HeaderTemplate = dataTemplate;
DataGrid1.Columns.Add(column);
然后我要访问labelA和labelB并更改内容。
您不能在运行时更改模板的内容,除非您希望使用该模板的每个项目都更改内容。 p>
在您的情况下,我将根据需要创建标题。您可以通过将代码放在自己的方法中来创建头文件来使其更容易。
public void AddColumnHeader(DataGridTextColumn column,string header1,string header2)
{
var panel = new StackPanel();
var labelA = new Label();
labelA.Content = header1;
panel.Children.Add(labelA);
var separator = new Separator();
separator.HorizontalAlignment = HorizontalAlignment.Stretch;
panel.Children.Add(separator);
var labelB = new Label();
labelB.Content = header2;
panel.Children.Add(labelB);
column.Header = panel;
}
然后应用您的标题,只需使用
var column = new DataGridTextColumn();
AddColumnHeader(column);
DataGrid1.Columns.Add(列,标签内容1,标签内容2);
I'm dynamically creating DataGrid columns (based on an event from my ViewModel) and programmatically adding them to an existing DataGrid. Each column uses a generic HeaderTemplate by setting it to a DataTemplate that has been identified in the xaml. The DataTemplate contains two labels in which needs their content needs to be changed upon creation of the DataGrid column. How would this be done? I understand that the DataTemplate uses the ContentPresenter but I am having trouble accessing it within a dynamically created DataGrid column. Code is as follows:
xaml: (template used to format the DataGrid column header):
<DataTemplate x:Key="columnTemplate">
<StackPanel>
<Label Padding="0" Name="labelA"/>
<Separator HorizontalAlignment="Stretch"/>
<Label Padding="0" Name="labelB"/>
</StackPanel>
</DataTemplate>
c#: (used to dynamically create a DataGrid column and add it to an existing DataGrid)
var dataTemplate = FindResource("columnTemplate") as DataTemplate;
var column = new DataGridTextColumn();
column.HeaderTemplate = dataTemplate;
DataGrid1.Columns.Add(column);
I would like to then access both labelA and labelB and change the content.
You can't change the contents of a template at runtime unless you want every item which uses that template to have it's contents changed as well.
In your situation, I would just create the header as needed. You can make it easier by putting the code to create the Header in it's own method.
public void AddColumnHeader(DataGridTextColumn column, string header1, string header2)
{
var panel = new StackPanel();
var labelA = new Label();
labelA.Content = header1;
panel.Children.Add(labelA);
var separator = new Separator();
separator.HorizontalAlignment = HorizontalAlignment.Stretch;
panel.Children.Add(separator);
var labelB = new Label();
labelB.Content = header2;
panel.Children.Add(labelB);
column.Header = panel;
}
Then to apply your header, just use
var column = new DataGridTextColumn();
AddColumnHeader(column);
DataGrid1.Columns.Add(column, "label content 1", "label content 2");
这篇关于从DataGrid列标题中使用的DataTemplate中以编程方式更改标签内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!