0

I've an xaml code in which I've used DataTemplateSelector.But it shows me namespace error.Code behind for it is written inside "TimeSheet.Views.DataTemplate" namespace and xaml code is written in "TimeSheet.Views" namespace.How should I write namespace for it?

outline of my xaml code is:

<Controls:MetroWindow 
    x:Name="MainWin"
    x:Class="TimeSheet.DayView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:l="clr-namespace:TimeSheet.Views.DataTemplateSpace"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    Title="DayView" Width="590" Height="590">

<l:DayViewListDataTemplateSelector x:Key="templateSelector"
          DefaultDataTemplate="{StaticResource DefaultDataTemplate}"
          EditableDataTemplate="{StaticResource EditableDataTemplate}"/>

Code Behind for it is,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
//using System.Windows.DependencyProperty;
//using System.ComponentModel.DependencyPropertyDescriptor;
using HarvestApp;

namespace TimeSheet.Views.DataTemplateSpace
{
public class DayViewListDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate DefaultDataTemplate { get; set; }
    public DataTemplate EditableDataTemplate { get; set; }
    //public DataTemplate EnumDataTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item,DependencyObject container)
    {
      //some code
    }
}
}
4

1 回答 1

0

这是答案:

    <Controls:MetroWindow 
        x:Name="MainWin"
        x:Class="TimeSheet.DayView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:l="clr-namespace:TimeSheet.Views.DataTemplateSpace"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        Title="DayView" Width="590" Height="590">

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
            </ResourceDictionary.MergedDictionaries>

    <DataTemplate x:Key="DefaultDataTemplate"> //here you must write key 
    <StackPanel Orientation="Horizontal">
     <TextBox Text="{Binding ClientNameBinding}" Background="Transparent" Padding="0"    Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="145"/>
                    <TextBox Text="{Binding ApplicationNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
                    <TextBox Text="{Binding StartTimeBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="100"/>
                    <TextBox Text="{Binding StopTimeBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="60"/>
                    <TextBox Text="{Binding ProjectNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="130"/>
                    <TextBox Text="{Binding TaskNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="71"/>
                </StackPanel>
            </DataTemplate>

        <!-- Editable DataTemplate -->
       <DataTemplate x:Key="EditableDataTemplate">   //here you must write key 
       <StackPanel Orientation="Horizontal">
                <TextBox Text="{Binding ClientNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="145"/>
                <TextBox Text="{Binding ApplicationNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
                <TextBox Text="{Binding StartTimeBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="100"/>
                <TextBox Text="{Binding StopTimeBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="60"/>
                <TextBox Text="{Binding TaskNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="130"/>
                <ComboBox ItemsSource="{Binding projectList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name" SelectedValue="{Binding selectedProjectid}" />                    
            </StackPanel>
        </DataTemplate>

<l:DayViewListDataTemplateSelector x:Key="templateSelector"
              DefaultDataTemplate="{StaticResource DefaultDataTemplate}"
              EditableDataTemplate="{StaticResource EditableDataTemplate}"/>

        </ResourceDictionary>

    </Window.Resources>
于 2013-06-20T06:07:36.197 回答