1

i have build my own usercontrol template, the inherited class looks like:

using System.Windows.Controls;
using CustomCopyNas.Views;

namespace CustomCopyNas.MVVM
{
    /// <summary>
    /// Base class for all Views that is used in MVVM
    /// </summary>
    /// <typeparam name="TViewModel">ViewModel</typeparam>
    public class ViewBase<TViewModel> : UserControl, IView<TViewModel> where TViewModel : UploadViewModelBase
    {
        public ViewBase()
        { }

        public ViewBase(TViewModel tViewModel)
        {
            ViewModel = tViewModel;
        }

        /// <summary>
        /// ViewModel
        /// </summary>
        public TViewModel ViewModel
        {
            get
            {
                return (TViewModel)DataContext;
            }
            private set
            {
                DataContext = value;
            }
        }
    }
}

my xaml file

<mvvm:ViewBase x:Class="CustomCopyNas.Controls.FolderControl"
             x:TypeArguments="vm:FolderViewModel"
             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:mvvm="clr-namespace:CustomCopyNas.MVVM"
             xmlns:vm="clr-namespace:CustomCopyNas.Views"
             xmlns:enum="clr-namespace:CustomCopyNas.Enum"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Width="700">

    <mvvm:ViewBase.Resources>
        <ObjectDataProvider x:Key="osEnum" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type Type="enum:OsType"></x:Type>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </mvvm:ViewBase.Resources>


    <Grid>
        <DataGrid ItemsSource="{Binding Folders, Mode=TwoWay}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Folder or File" Binding="{Binding Path}"/>
            </DataGrid.Columns>
        </DataGrid>    
    </Grid>
</mvvm:ViewBase>

and partial class

using CustomCopyNas.MVVM;
using CustomCopyNas.Views;

namespace CustomCopyNas.Controls
{
    /// <summary>
    /// Interaction logic for FolderControl.xaml
    /// </summary>
    public partial class FolderControl : ViewBase<FolderViewModel>
    {

        public FolderControl()
            : base(new FolderViewModel("SourceFolders.xml"))
        {
            InitializeComponent();
        }
    }
}

When i am trying to compile, i've got error, that the property resources does not exist on viewbase. I counld not figure out, where the error is and my viewbase class is inherited from usercontrol class, this provide resources property.
What is here wrong?

softshipper
  • 32,463
  • 51
  • 192
  • 400

1 Answers1

2

I believe that your problems are caused because generic classes are not supported in XAML. You can read the full story in the Generics in XAML page on MSDN, but in short, from the linked page:

In XAML, a generic type must always be represented as a constrained generic; an unconstrained generic is never present in the XAML type system or a XAML node stream and cannot be represented in XAML markup.

As such, you have a mismatch between your code class declaration:

public class ViewBase<TViewModel>

And your XAML class declaration:

<mvvm:ViewBase x:Class="CustomCopyNas.Controls.FolderControl"

The fact that they do not match will cause you a variety of problems.

Please see the Can I specify a generic type in XAML? and WPF UserControl with generic code-behind questions for further information on this subject.

Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183