简体   繁体   English

WPF按代码编辑模板列

[英]WPF Edit Templated Column by Code

I'm trying to edit the contents of an StackPanel inside a templated DataGrid Column by Code. 我正在尝试通过代码在模板化的DataGrid列内编辑StackPanel的内容。 Unfortunately i cannot find the StackPanel from Code. 不幸的是,我无法从代码中找到StackPanel。 Can anybody help me, please? 有人可以帮我吗?

This is my DataTemplate: 这是我的DataTemplate:

<UserControl.Resources>
    <DataTemplate x:Key="ReservationContainerTemplate">
        <StackPanel Orientation="Horizontal" Background="Black" />
    </DataTemplate>
</UserControl.Resources>

This is how I create this Column: 这就是我创建此列的方式:

var colReservations = new DataGridTemplateColumn();

colReservations.Header = "Nordplatz";

DataTemplate dt = null;
dt = dataGrid1.FindResource("ReservationContainerTemplate") as DataTemplate;

colReservations.CellTemplate = dt;
dataGrid1.Columns.Add(colReservations);

What I need to do is, writing to this StackPanel inside DataTemplate. 我需要做的是在DataTemplate内部写入此StackPanel。

I was able to get the Object by following Code: 我可以通过以下代码获取对象:

        DataRowView n = (DataRowView)dataGrid1.Items[i];
        //var m = dataGrid1.SelectedItem.Cells[0].Text;

        DataTemplate Template = dataGrid1.FindResource("ReservationContainerTemplate") as DataTemplate;
        StackPanel stp = Template.LoadContent() as StackPanel;

Great, I've got my Object, but how can I modify it? 太好了,我有我的对象,但是我该如何修改呢? Under this conditions i've only got a copy of the Object, changes are not reflected to the original one. 在这种情况下,我只有对象的一个​​副本,更改不会反映到原始对象上。

Does anybody have got an idea? 有人知道吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM