I'm learning WPF and XAML. Here is my Form XAML code and the code for my class (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>
Here are the errors
after correcting the xmlns:src, I get the following Warning:
Warning 1 ''src' is an undeclared prefix. Line 8, position 14.' XML is not valid. C:\Bail\Bail\Bail\MainWindow.xaml 8 14 Bail
The error refers to this line in the XAML (<src:ListboxMenuItems x:Key="ListboxMenuItems"/>
)
ListBoxMenuItems is a class I created in C#.
Here;s the code to the class
//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"));
}
}
}
your xmlns declaration at the top should include src:
xmlns:src="clr-namespace:Bail"
Also, it looks like you'll need to point your ItemsSource of your ListBox to Items within your class. I would suggest changing your class to just inherit from List and do away with the Items property:
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"));
}
}
This should make it work.
The line
xmlns="clr-namespace:Bail"
must be changed to
xmlns:src="clr-namespace:Bail"
Edit:
After making the ListboxMenuItems
Class IEnumerable
and IEnumerator
the Code works for me:
//FileName: ListboxMenuItems.cs
using System;
using System.Collections;
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;
}
}
public class ListboxMenuItems : IEnumerable, IEnumerator
{
List<ListboxMenuItem> Items { get; set; }
private int _position = -1;
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"));
}
#region Implementation of IEnumerable
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
/// <filterpriority>2</filterpriority>
public IEnumerator GetEnumerator()
{
return this;
}
#endregion
#region Implementation of IEnumerator
/// <summary>
/// Advances the enumerator to the next element of the collection.
/// </summary>
/// <returns>
/// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
/// </returns>
/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception><filterpriority>2</filterpriority>
public bool MoveNext()
{
_position++;
return (_position < Items.Count);
}
/// <summary>
/// Sets the enumerator to its initial position, which is before the first element in the collection.
/// </summary>
/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception><filterpriority>2</filterpriority>
public void Reset()
{
_position = -1;
}
/// <summary>
/// Gets the current element in the collection.
/// </summary>
/// <returns>
/// The current element in the collection.
/// </returns>
/// <exception cref="T:System.InvalidOperationException">The enumerator is positioned before the first element of the collection or after the last element.</exception><filterpriority>2</filterpriority>
public object Current
{
get
{
try
{
return Items.ElementAt(_position);
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
#endregion
}
}
src
is not defined as a namespace prefix in your XAML file.
You probably need to change line 4 to:
xmlns:src="clr-namespace:Bail"
精彩评论