Infragistics XamDatagrid - 防止控件在不可见时被实例化
我们有一个 Infragistics XamDatagrid,其中有一列包含图像。
该图像可以可见或不可见,具体取决于 ViewModel 属性。当它可见时,如果将鼠标悬停在其上,则会显示一个弹出窗口。请参阅下面此列的 XAML。
<Setter Property="IsEnabled" Value="true" />
<Setter Property="Focusable" Value="false" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataPresenter:CellValuePresenter}">
<Border Background="{StaticResource FrozenColumnBrush}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Image x:Name="BarrierImage"
Grid.Row="0"
Height="16" Width="16"
HorizontalAlignment="Center" VerticalAlignment="Center"
Source="../Images/lock_16.ico"
Visibility="{Binding DataItem.IsBarrierEvent, Converter={StaticResource BooleanToVisibilityConverter}}" >
</Image>
<Grid Grid.Row="1" Visibility="{Binding DataItem.IsBarrierEvent, Converter={StaticResource BooleanToVisibilityConverter}}" >
<Popup
Name="BarrierPopup"
AllowsTransparency="True"
PopupAnimation="Fade"
HorizontalOffset="-35"
VerticalOffset="0"
Behaviors:OpenPopupWhenTooltipOpening.Control="{Binding ElementName=BarrierImage}"
>
<Views:BarrierView DataContext="{Binding DataItem.Barriers}"/>
</Popup>
</Grid>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
问题是我们得到了 200 行,但只有一行的 IsBarrierEvent = true,因此只有一张图像,并且悬停时只会显示一个弹出窗口。但事实证明,WPF 为每一行创建一个 BarrierView 控件。我们在使用 dotTrace 调查性能问题时注意到了这一点。
有什么方法可以防止创建这种不可见的控件吗?我们还尝试将 viewmodel 属性绑定到主边框可见性,但它们仍在创建中。
谢谢。
PS - 我尝试粘贴样式的 XAML 标记,但它没有正确显示
We've got an Infragistics XamDatagrid with a column that contains an image.
This image can be visible or not depending on a ViewModel property. When it is visible, if you hover over, a popup is displayed. Please see below the XAML for this column.
<Setter Property="IsEnabled" Value="true" />
<Setter Property="Focusable" Value="false" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataPresenter:CellValuePresenter}">
<Border Background="{StaticResource FrozenColumnBrush}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Image x:Name="BarrierImage"
Grid.Row="0"
Height="16" Width="16"
HorizontalAlignment="Center" VerticalAlignment="Center"
Source="../Images/lock_16.ico"
Visibility="{Binding DataItem.IsBarrierEvent, Converter={StaticResource BooleanToVisibilityConverter}}" >
</Image>
<Grid Grid.Row="1" Visibility="{Binding DataItem.IsBarrierEvent, Converter={StaticResource BooleanToVisibilityConverter}}" >
<Popup
Name="BarrierPopup"
AllowsTransparency="True"
PopupAnimation="Fade"
HorizontalOffset="-35"
VerticalOffset="0"
Behaviors:OpenPopupWhenTooltipOpening.Control="{Binding ElementName=BarrierImage}"
>
<Views:BarrierView DataContext="{Binding DataItem.Barriers}"/>
</Popup>
</Grid>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
The problem is that we get 200 rows, but only one has IsBarrierEvent = true, therefore there's only one image and only one popup will be displayed when hovering. But it turns out that WPF creates a BarrierView control for each row. We've noticed that when using dotTrace to investigate performance issues.
Is there any way to prevent this non-visible controls to be created? We've tried to also bind the viewmodel property to the main border visibility, but they're still being created.
Thanks.
P.S - I tried to paste in the XAML tags for the Style, but it wasn't properly displayed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您创建自己的自定义控件并将图像、弹出窗口等移动到该控件的模板中。当您在模板中定义了元素并且应用了该模板时,将创建该模板中的所有元素,因此在这种情况下,当应用 CVP 模板时,这意味着甚至会创建 Image、Popup、BarrierView 等如果模板内的祖先元素被折叠。但是,如果模板中有一个其可见性已折叠的控件,则该控件的模板将不会应用,直到首次测量该控件的模板,而在可见性为隐藏或可见之前不会发生这种情况。
I would recommend that you create your own custom control and move the Image, Popup, etc. into the template of that control. When you have elements defined in a template and that template is applied all of the elements within that template will be created so in this case when the CVP template is applied that means that Image, the Popup, the BarrierView, etc. will be created even if an ancestor element within the template is collapsed. However if you have a control in the template whose Visibility is Collapsed, the template of that control would not be applied until it is first measured which won't happen until the Visibility is Hidden or Visible.