【问题标题】:Different UserControl inside a WPF ListBoxWPF列表框中的不同用户控件
【发布时间】: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)吗?很难说出了什么问题,因为当我出于示例目的稍微简化类时,您的代码对我来说很好。
  • 您必须确保值是12。否则,不存在可供选择的默认模板以防条件失败,WPF 将显示实际数据类型的 ToString() 结果(默认情况下是完全限定的类型名称)。如果值可以不是 @ 987654326@ 和 2 您必须分配一个默认模板。
  • 您应该考虑扩展 DataTemplateSelector 而不是实现这些触发器。
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: c# wpf listbox


【解决方案1】:

您正在使用DataTemplate : 'CopyMoveView' 作为ContentPresenterBgTasksContentStyle 中的ControlTemplate。考虑使用UIElement,因为DataTemplate 不是。在此处查看示例:Here

还有你为什么使用ContentPresenter我建议你使用ContentControl

【讨论】:

    【解决方案2】:

    BionicCode 提出的解决方案很有启发性。 经过一些尝试,我发现在BackgroundTaskType 和用作触发器的值之间缺少一个转换器。

    下面几行代码解决了这个问题。

    <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>
    

    感谢您的支持!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      相关资源
      最近更新 更多