• Home
  • »
  • WPF
  • »
  • Listbox, DepedencyProperty, primary questions
AuthorQuestion
Listbox, DepedencyProperty, primary questions
Posted on: 13 Dec 2012
Hello, I've started to programming Aplplications in WPF and I have very simple questions, because I don't understand most of courses in the Internet.


1. What is DependencyProperty and how can I use it for example - it is to change parameters in XAML code ?


2. I have some Listboxes and I'd like to add some entires to list by this application - how can I do it ?


XAML:


<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel x:Name="sp_gridcolumn1" Grid.Column="1">
<ListBox Name="listboks" ItemsSource="{Binding lista}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding imie}" Grid.Column="0"/>
<TextBlock Text="{Binding nazwisko}" Grid.Column="1"/>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"
Background="Beige">
</StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</StackPanel>
<StackPanel Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center">
<Label>
Imie
</Label>
<TextBox Name="imiee" Margin="0 5" >
</TextBox>
<Label>
Nazwisko
</Label>
<TextBox Name="nazwiskoo" Margin="0 5">
</TextBox>
 <Button Click="add" Height="30" Width="100"> 
Dodaj
</Button>
 <Button Click="remove" Height="30" Width="100" Margin="5"> 
   remove
</Button>
 <Button Click="clear" Height="30" Width="100"> 
  clear 
</Button>
 <Button Click="exit" Height="30" Width="100" Margin="0 10"> 
  exit 
</Button>
</StackPanel>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
</Grid>
</Window>


C# code:




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
CompositeCollection[] lista = new CompositeCollection[]
{
new CompositeCollection{imie = "Janusz", nazwisko = "Nowak"},
new CompositeCollection{imie = "Jan", nazwisko = "Kowalski"}
};
public MainWindow()
{
InitializeComponent();
listboks.ItemsSource = lista;
}
private void dodaj(object sender, RoutedEventArgs e)
{
if ((imiee.Text != "") && (nazwiskoo.Text != ""))
{
listboks.Items.Add(imiee.Text);
listboks.Items.Add(nazwiskoo.Text);
}
else MessageBox.Show("Podaj imie oraz nazwisko!");
}
 private void clear(object sender, RoutedEventArgs e) 
{
listboks.ItemsSource.GetHashCode();
}
 private void exit(object sender, RoutedEventArgs e) 
{
this.Close();
}
 private void remove(object sender, RoutedEventArgs e) 
{
}
}
class CompositeCollection
{
public string imie
{
get;
set;
}
public string nazwisko
{
get;
set;
}
}
}






Please, give me only simple examples


Thanks. 

[ + ]
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter