WPF:需要帮助使用 DataGridTemplateColumn.CellTemplate 在 DataGrid 中添加 UserControl

发布于 2024-12-21 02:47:16 字数 2119 浏览 4 评论 0原文

我有一个 WPF 用户控件。我想在 DataGrid 中每行的第一列中添加该用户控件。我已经使用DataTemplate来实现这一点。当用户在 DataGrid 最后一行中按 ENTER 键时,我添加一个新行。 Organization:OrganizationPicker 是下面的 UserControl。

<DataGridTemplateColumn Width="250" Header="{x:Static resx:Resources.Organisation}" x:Name="OrgPickerColumn" CellStyle="{StaticResource SingleClickEditing}">                                
  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <Grid >
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" Name="column1"></ColumnDefinition>
          <!--<ColumnDefinition Width="0.1*"></ColumnDefinition>-->
        </Grid.ColumnDefinitions>

        <Organisation:OrganisationPicker Width="240"
                                         HorizontalAlignment="Left" Margin="2,2,2,2"
                                         Name="orgPicker"
                                         VerticalAlignment="Top"
                                         Grid.Column="0"
                                         IsValidSearch="{Binding OrganisationError,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=true, NotifyOnValidationError=true,NotifyOnSourceUpdated=True}" 
                                         SelectedOrganisationalUnit="{Binding Path=ReqOrganisationId,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=true, NotifyOnValidationError=true,NotifyOnSourceUpdated=True}"
                                         Foreground="{DynamicResource ContentForeground}" FontFamily="{DynamicResource DefaultFontFamily}" FontSize="{DynamicResource NormalFontSize}">
        </Organisation:OrganisationPicker>                                           
      </Grid>
    </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

添加用户控件,但是当输入新行时,用户控件的构造函数被调用的次数与网格中的行数一样多,显然我丢失了已经存在的用户控件实例的状态当前行。

例如,如果添加的新行是网格中的第 5 行,则构造函数将被调用 5 次!

为什么会发生这种情况?我怎样才能防止这种情况发生?

I have a WPF UserControl. I want to add that user control in first column of every row in the DataGrid. I have used DataTemplate to achieve this. I add a new row when user hits ENTER key in the last row of the DataGrid. Organisation:OrganisationPicker is the UserControl below.

<DataGridTemplateColumn Width="250" Header="{x:Static resx:Resources.Organisation}" x:Name="OrgPickerColumn" CellStyle="{StaticResource SingleClickEditing}">                                
  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <Grid >
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" Name="column1"></ColumnDefinition>
          <!--<ColumnDefinition Width="0.1*"></ColumnDefinition>-->
        </Grid.ColumnDefinitions>

        <Organisation:OrganisationPicker Width="240"
                                         HorizontalAlignment="Left" Margin="2,2,2,2"
                                         Name="orgPicker"
                                         VerticalAlignment="Top"
                                         Grid.Column="0"
                                         IsValidSearch="{Binding OrganisationError,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=true, NotifyOnValidationError=true,NotifyOnSourceUpdated=True}" 
                                         SelectedOrganisationalUnit="{Binding Path=ReqOrganisationId,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=true, NotifyOnValidationError=true,NotifyOnSourceUpdated=True}"
                                         Foreground="{DynamicResource ContentForeground}" FontFamily="{DynamicResource DefaultFontFamily}" FontSize="{DynamicResource NormalFontSize}">
        </Organisation:OrganisationPicker>                                           
      </Grid>
    </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

User control gets added but when new row is entered, constructor of the user control is called as many times as number of rows in the grid and obviously I lose the state of the user control instances that are in already present rows.

e.g. If new row thats being added is the 5th row in grid, then constructor is called 5 times!

Why does it happen? How can I prevent it to happen?

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

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

发布评论

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

评论(2

几味少女 2024-12-28 02:47:16

问题是视图模型没有正确编写。每次添加新行时,绑定到网格的集合都会重新初始化。所以我修复了视图模型并且它起作用了。

感谢大家在这里发表评论。

我正在关闭这个问题,因为解决方案太本地化,无法在在线论坛上识别问题。

The problem was that view model was not written correctly. The collection which is bound to the grid was reinitialized everytime new row is added. So I fixed the view model and it worked.

Thanks to all for contributing your comments here.

I am closing this question because solution was too local and it was impossible to identify problem on online forum.

心房的律动 2024-12-28 02:47:16

你说你失去了控制的状态。

我认为您必须将控件的相关数据存储在 ui/control 后面的视图模型中(每个绑定等)。

您的控件/列只是数据的呈现者。

我不知道网格到底是如何工作的,但我认为通过网格中的虚拟化,您的控件将按照其获取控件的频率生成。

希望这有帮助。

You say you loose the state for your control.

I think you must store the relevant data for your control at the view model behind your ui/control (per binding etc.).

Your control/column is only a presenter for your data.

I don't know how exactly the grid works, but I think by the virtualization in the Grid, your control will be generated as often as it takes the control.

Hope this helps.

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