【发布时间】:2021-12-30 08:49:54
【问题描述】:
我正在尝试在带有触发器的 WPF ListBox 中显示不同的 UserControl。
我尝试过这种方法,但没有成功。
<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>
在列表框中,我可以看到模型 FileManager.ViewModel.BackgroundOperationsModel.MoveCopyDialogModel 的完整命名空间,但未呈现组件。
有什么建议吗?
【问题讨论】:
-
这是一个 ObservableCollection
-
你能发布更多
BackgroundOperations中的类模型(我猜是MoveCopyDialogModel)吗?很难说出了什么问题,因为当我出于示例目的稍微简化类时,您的代码对我来说很好。 -
您必须确保值是
1或2。否则,不存在可供选择的默认模板以防条件失败,WPF 将显示实际数据类型的 ToString() 结果(默认情况下是完全限定的类型名称)。如果值可以不是 @ 987654326@ 和2您必须分配一个默认模板。 -
您应该考虑扩展 DataTemplateSelector 而不是实现这些触发器。
-
请提供足够的代码,以便其他人更好地理解或重现问题。