c# - WPF 窗体设计器与我的 XAML 有问题。请帮忙

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

我正在学习 WPF 和 XAML。这是我的表单 XAML 代码和我的类 (ListboxMenuItem) 的代码

<Window x:Class="Bail.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="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:\Bail\Bail\Bail\MainWindow.xaml 8 14 保释金

错误指的是 XAML 中的这一行 ( <src:ListboxMenuItems x:Key="ListboxMenuItems"/> )

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")); 
        } 
    }
}

最佳答案

你在顶部的 xmlns 声明应该包括 src:

xmlns:src="clr-namespace:Bail"

此外,您似乎需要将 ListBox 的 ItemsSource 指向您的类中的 Items。我建议将您的类更改为仅继承 List 并取消 Items 属性:

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

这应该可以正常工作。

关于c# - WPF 窗体设计器与我的 XAML 有问题。请帮忙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6358598/

相关文章:

c# - 等效的 Java 泛型循环引用 C#

c# - Windows 服务中的 Kerberos 身份验证

wpf - 在一个 WPF 网格单元中有多个控件

c# - 加载 DLL 创建实例并卸载

c# - 用于多列使用的 ListView 与 ListBox

visual-studio-2010 - 在 Visual Studio 中,如何编写将日期插入评论的宏?

c# - 为什么这个 Visual Studio 计时器组件在设计时工作?

c# - 如何将参数传递给 WCF 数据服务并根据它切换连接字符串?

wpf - 即使在 wpf C# 中禁用了控件,如何获得 100% 的不透明度

c++ - 使用 VS2010 创建 C++ 非托管 DLL