Mega Code Archive
Create a ListBoxItem, set font, content, add the ListBoxItem to the ListBox
List Item 1
List Item 2
List Item 3
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void btnAddListItem_Click(object sender, RoutedEventArgs e)
{
if (textBox.Text.Length == 0)
{
MessageBox.Show("Enter text to add to the list.", Title);
}
else
{
ListBoxItem item = new ListBoxItem();
item.Content = textBox.Text;
item.IsSelected = true;
item.HorizontalAlignment = HorizontalAlignment.Center;
item.FontWeight = FontWeights.Bold;
item.FontFamily = new FontFamily("Tahoma");
listBox1.Items.Add(item);
textBox.Clear();
textBox.Focus();
}
}
private void btnDeleteListItem_Click(object sender, RoutedEventArgs e)
{
if (listBox1.SelectedItems.Count == 0)
{
MessageBox.Show("Select list items to delete.", Title);
}
else
{
while (listBox1.SelectedItems.Count > 0)
{
listBox1.Items.Remove(listBox1.SelectedItems[0]);
}
}
}
private void btnSelectAll_Click(object sender, RoutedEventArgs e)
{
listBox1.SelectAll();
}
}
}