繁体   English   中英

WPF 列表框内的不同用户控件

[英]Different UserControl inside a WPF ListBox

我试图在带有触发器的 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>

在列表框中,我可以看到 model FileManager.ViewModel.BackgroundOperationsModel.MoveCopyDialogModel 的完整命名空间,但该组件未呈现。

有什么建议么?

先谢谢了

您正在使用DataTemplate : 'CopyMoveView' 作为BgTasksContentStyleContentPresenterControlTemplate 考虑使用UIElement ,因为DataTemplate不是。 请参阅此处的示例: 此处

另外你为什么使用ContentPresenter我建议你使用ContentControl

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>

谢谢你的支持!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM