WPF设计器代码一直告诉我有错误

时间:2011-06-14 14:01:59

标签: c# wpf visual-studio-2010 xaml

这是我的Form XAML代码。

<Window x:Class="Bail.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns="clr-namespace:Bail"
        Title="MainWindow" Height="768" Width="1024" WindowStartupLocation="CenterScreen"
        Closing="Window_Closing" ResizeMode="NoResize">
    <Grid>
        <Grid.Resources> 
            <src:ListboxMenuItems x:Key="ListboxMenuItems"/>
        </Grid.Resources>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="185" />
            <!-- Or Auto -->
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>


        <ListBox Width="150" Margin="0,5,0,10" Grid.Column="0"
                 ItemsSource="{StaticResource ListboxMenuItems}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Padding="5,0,5,0" Text="{Binding FirstName}" />
                        <TextBlock Text="{Binding LastName}" />
                        <TextBlock Text=", " />
                        <TextBlock Text="{Binding Address}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>            
        </ListBox>


        <Canvas Grid.Column="1" />
    </Grid>
</Window>

以下是错误


在更正xmlns:src后,我得到以下内容 警告:

警告1''src'是未声明的前缀。第8行,第14位。' XML无效。 C:\ Users \ Shayaan Siddiqui \ Documents \ Bail \ Bail \ Bail \ MainWindow.xaml 8 14 Bail

ListBoxMenuItems是我在C#中创建的类。

这里;是类

的代码
//FileName: ListboxMenuItems.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bail
{
    public class ListboxMenuItem
    {
        public String FirstName { get; set; }
        public String LastName { get; set; }
        public String Address { get; set; }

        public ListboxMenuItem(String firstName, String lastName, String address)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Address = address;
        }
    }

    class ListboxMenuItems 
    { 
        List<ListboxMenuItem> Items { get; private set; } 
        public ListboxMenuItems() 
        { 
            Items = new List<ListboxMenuItem>(); 
            Items.Add(new ListboxMenuItem("Michael", "Anderberg", "12 North Third Street, Apartment 45")); 
            Items.Add(new ListboxMenuItem("Chris", "Ashton", "34 West Fifth Street, Apartment 67")); 
            Items.Add(new ListboxMenuItem("Cassie", "Hicks", "56 East Seventh Street, Apartment 89")); 
            Items.Add(new ListboxMenuItem("Guido", "Pica", "78 South Ninth Street, Apartment 10")); 
        } 
    }
}

3 个答案:

答案 0 :(得分:3)

的xmlns:SRC = “CLR-名称空间:ListBoxSnippetEx”

您的代码中缺少此行。将ListBoxSnippetEx替换为项目命名空间。

答案 1 :(得分:2)

您在Window标记中缺少名称空间声明:

<window x:Class="Bail.MainWindow" 
        xmlns:src="clr-namespace:Bail" 
        .../>

另外,如果您更改了对象的名称以删除末尾的“s”,则还需要在xaml中更改它。

<Grid.Resources> 
    <src:ListboxMenuItem x:Key="ListboxMenuItems"/> 
</Grid.Resources>

答案 2 :(得分:2)

你还有额外的&lt;字符第8行:

    <Grid.Resources> 
        < 
        <src:ListboxMenuItems x:Key="ListboxMenuItems"/>