[英]Different UserControl inside a WPF ListBox
I'm trying to show different UserControl inside a WPF ListBox with trigger.我试图在带有触发器的 WPF ListBox 中显示不同的 UserControl。
I've try this approach, but with no luck.我尝试过这种方法,但没有运气。
<UserControl
x:Class="FileManager.View.BackgroundOperationDialog.BackgroundOperationDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FileManager.View.BackgroundOperationDialog"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<UserControl.Resources>
<DataTemplate x:Key="CopyMoveView">
<local:MoveCopyDialog OperationDetails="{Binding}" ShowAllDetails="False" />
</DataTemplate>
<DataTemplate x:Key="ReductionTask">
<local:ReductionTask />
</DataTemplate>
<Style x:Key="BgTasksContentStyle" TargetType="ContentPresenter">
<Style.Triggers>
<DataTrigger Binding="{Binding RowData.Row.BackgroundTaskType}" Value="1">
<Setter Property="ContentTemplate" Value="{StaticResource ReductionTask}" />
</DataTrigger>
<DataTrigger Binding="{Binding RowData.Row.BackgroundTaskType}" Value="2">
<Setter Property="ContentTemplate" Value="{StaticResource CopyMoveView}" />
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<ListBox ItemsSource="{Binding BackgroundOperations}">
<ListBox.ItemTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding}" Style="{StaticResource BgTasksContentStyle}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>
In the listbox I can see the full namespace of the model FileManager.ViewModel.BackgroundOperationsModel.MoveCopyDialogModel but the component is not rendered.在列表框中,我可以看到 model FileManager.ViewModel.BackgroundOperationsModel.MoveCopyDialogModel 的完整命名空间,但该组件未呈现。
Any suggestions?有什么建议么?
Thanks in advanced先谢谢了
You are using the DataTemplate
: 'CopyMoveView' as the ControlTemplate
of ContentPresenter
in BgTasksContentStyle
.您正在使用DataTemplate
: 'CopyMoveView' 作为BgTasksContentStyle
中ContentPresenter
的ControlTemplate
。 Consider using a UIElement
as DataTemplate
is not.考虑使用UIElement
,因为DataTemplate
不是。 See example here: Here请参阅此处的示例: 此处
Also why are you using ContentPresenter
I'd advice you use ContentControl
另外你为什么使用ContentPresenter
我建议你使用ContentControl
The solution proposed by BionicCode was illuminating. BionicCode 提出的解决方案很有启发性。 After some attempts, I found that was missing a converter between BackgroundTaskType
and the value used as trigger.经过一些尝试,我发现在BackgroundTaskType
和用作触发器的值之间缺少一个转换器。
The following lines of code solve the problem.以下代码行解决了这个问题。
<DataTrigger Binding="{Binding RowData.Row.BackgroundTaskType, Converter={StaticResource bgTaskConverter}}" Value="1">
<Setter Property="ContentTemplate" Value="{StaticResource ReductionTask}" />
</DataTrigger>
<DataTrigger Binding="{Binding RowData.Row.BackgroundTaskType, Converter={StaticResource bgTaskConverter}}" Value="2">
<Setter Property="ContentTemplate" Value="{StaticResource CopyMoveView}" />
</DataTrigger>
Thanks for your support!谢谢你的支持!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.