【问题标题】:WPF: Need help in adding UserControl in DataGrid using the DataGridTemplateColumn.CellTemplateWPF:在使用 DataGridTemplateColumn.CellTemplate 在 DataGrid 中添加 UserControl 时需要帮助
【发布时间】:2011-12-13 12:56:26
【问题描述】:

我有一个 WPF 用户控件。我想在DataGrid 的每一行的第一列中添加该用户控件。我使用DataTemplate 来实现这一点。当用户在 DataGrid 的最后一行中按 ENTER 键时,我添加了一个新行。 Organisation:OrganisationPicker 是下面的 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 次!

为什么会这样?我怎样才能防止它发生?

【问题讨论】:

  • 我最好的猜测是,当添加新项目时,整个 DataGrid 正在重新绘制。也许您可以向我们展示添加新项目的代码? WPF 故意卸载不需要的控件,所以如果一个属性没有绑定到任何东西,那么它将失去它的价值。将您的选择绑定到某些东西是一种很好的做法,因为即使是最小化您的应用程序并恢复它之类的操作也会重绘(和重置)未绑定的控件。
  • @Rachel:情况并非如此。 Grid 还有 4 个其他列,它们可以毫无问题地保留其数据/状态。很抱歉,这里很难发布代码,因为它非常复杂且规模庞大。如果您能进一步指导我,我将不胜感激。如果代码是绝对必要的,我会尝试在这里提取更小的块。
  • 你能显示当你按 Enter 时执行的那段代码吗?
  • 显示 DataGrid 和绑定的相关代码。 (清除任何视觉信息,例如边距、宽度、对齐方式等)

标签: wpf user-controls wpfdatagrid


【解决方案1】:

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

感谢大家在这里贡献您的 cmets。

我将关闭此问题,因为解决方案过于本地化,无法在在线论坛上识别问题。

【讨论】:

    【解决方案2】:

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

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

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

    我不知道网格究竟是如何工作的,但我认为通过Grid 中的虚拟化,您的控件将尽可能频繁地生成。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-04-16
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 2013-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      相关资源
      最近更新 更多