0
votes

In my free time I'am trying to refactor one project to full MVVM model and using WPF data binding, templating ... etc stuff. But here is the problem. I have this control which not applying data template.

    <UserControl
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:vm="clr-namespace:WPFCADCore.ViewModels.CADPrimitives;assembly=WPFCADCore"
                 xmlns:local="clr-namespace:WPFCADCoreControls.Views.Editors" x:Class="WPFCADCoreControls.Views.Editors.EditorBaseView"
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300"
                 d:DataContext="{d:DesignData Source=EditorBaseDesignData.xaml}">
        <UserControl.Resources>
            <DataTemplate DataType="{x:Type vm:CADLinePrimitiveViewModel}">
                <Line X1="{Binding Begin.X}" Y1="{Binding Begin.Y}" 
                  X2="{Binding End.X}" Y2="{Binding End.Y}"
                  Stroke="{Binding LineColor}" StrokeThickness="{Binding LineWidth}">
                </Line>
            </DataTemplate>
        </UserControl.Resources>
        <ItemsControl ItemsSource="{Binding Entities}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas Background="White"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </UserControl>

This is all I see Canvas with text string

So binding to source is working, but dataTemplate not applying automaticaly.

I tried to use DataType="vm:CADLinePrimitiveViewModel" DataType="CADLinePrimitiveViewModel" No results.

And I cant insert this template into ItemSource, because there will be different types of objects there, not only lines

ObservableCollection<ICADEntityViewModel> Entities

Where ICADEntityViewModel is interface

Any suggestions? Workarounds?

1
I tried this locally and it worked fine. Can you check your Output window if there are any errors that could cause issues?huserben
Set the ItemTemplate of ItemsControl.user1672994
As i said, I cant set itemTemplate in ItemsControl because there will be different types in source and templates should be applyied according types, besides that I want to define template in resourceDictionary, this example just for simplicity sake.Alexandr Denschikov

1 Answers

0
votes

Kinda answer was found, more like workaround. I was developing WPF Class Library, and when I created WPF application and copied code there it started to work right. Have no idea why it's not working in class library