C# 从c中的xaml DataGridTemplateColumn列创建#
我有简短的xaml代码:C# 从c中的xaml DataGridTemplateColumn列创建#,c#,xaml,C#,Xaml,我有简短的xaml代码: <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Category}"></TextBlock> </DataTemplate> </DataG
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Category}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Category}" KeyUp="TextBox_KeyUp"></TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
我想这可能会有帮助。我只是一步一步地用C代码转换XAML标记
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn
{
CellTemplate = new DataTemplate
{
DataType = typeof(TextBlock)
},
CellEditingTemplate = new DataTemplate
{
DataType = typeof(TextBox)
}
};
FrameworkElementFactory CategoryBlock = new FrameworkElementFactory(typeof(TextBlock));
CategoryBlock.SetBinding(TextBlock.TextProperty, new Binding("Category"));
templateColumn.CellTemplate.VisualTree = CategoryBlock;
FrameworkElementFactory CategoryTextBox = new FrameworkElementFactory(typeof(TextBox));
CategoryTextBox.SetBinding(TextBox.TextProperty, new Binding("Category"));
CategoryTextBox.AddHandler(KeyUpEvent, new KeyEventHandler(TextBox_KeyUp));
templateColumn.CellEditingTemplate.VisualTree = CategoryTextBox;
ProjectDataGrid.Columns.Add(templateColumn);
谢谢,我对采取的方法有问题。因为当它是公共的时,我获取方法,但不能创建委托。但当被保护时无法获得方法。当is protected method=null时,当is public delegat抛出时,访问与委托不兼容。这个方法应该是什么样子的。我试着使用其他的owerLoadGetMethod,但是没有用。好的,我找到了如何获取非公共方法,但是委托键控仍然不起作用。错误:无法绑定到目标方法,因为其安全签名或透明度与委派类型的安全签名或透明度不匹配。可能会有帮助:MethodInfo方法=this.GetType().GetMethod(“TextBox_KeyUp”,BindingFlags.NonPublic | BindingFlags.Instance);我加上去了。我更改了“Delegate KeyUp=Delegate.CreateDelegate(actionType,this,method);”。但是现在仍然抛出“CategoryTextBox.AddHandler(KeyUpEvent,KeyUp);”异常:“指定的强制转换无效”“当我尝试创建更多类似这样的对象时会发生这种情况。
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn
{
CellTemplate = new DataTemplate
{
DataType = typeof(TextBlock)
},
CellEditingTemplate = new DataTemplate
{
DataType = typeof(TextBox)
}
};
FrameworkElementFactory CategoryBlock = new FrameworkElementFactory(typeof(TextBlock));
CategoryBlock.SetBinding(TextBlock.TextProperty, new Binding("Category"));
templateColumn.CellTemplate.VisualTree = CategoryBlock;
FrameworkElementFactory CategoryTextBox = new FrameworkElementFactory(typeof(TextBox));
CategoryTextBox.SetBinding(TextBox.TextProperty, new Binding("Category"));
CategoryTextBox.AddHandler(KeyUpEvent, new KeyEventHandler(TextBox_KeyUp));
templateColumn.CellEditingTemplate.VisualTree = CategoryTextBox;
ProjectDataGrid.Columns.Add(templateColumn);