DataMmplateSelector的xaml中的命名空间错误

时间:2013-06-19 06:44:05

标签: xaml xml-namespaces datatemplateselector

我有一个xaml代码,其中我使用了DataTemplateSelector.But它显示了我的命名空间错误。后面的代码写在“TimeSheet.Views.DataTemplate”命名空间中,xaml代码写在“TimeSheet.Views”中如何为它编写命名空间?

我的xaml代码概述是:

<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}"/>

代码背后是,

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
    }
}
}

1 个答案:

答案 0 :(得分: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>