简体   繁体   中英

Different UserControl inside a WPF ListBox

I'm trying to show different UserControl inside a WPF ListBox with trigger.

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.

Any suggestions?

Thanks in advanced

You are using the DataTemplate : 'CopyMoveView' as the ControlTemplate of ContentPresenter in BgTasksContentStyle . Consider using a UIElement as DataTemplate is not. See example here: Here

Also why are you using ContentPresenter I'd advice you use ContentControl

The solution proposed by BionicCode was illuminating. After some attempts, I found that was missing a converter between BackgroundTaskType and the value used as trigger.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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