Reputation: 5618
I need exactly this: Click Me
But just for WPF.
And I know, that I should use Binding etc. , but its just for a own test.
I didn't find out yet how I can achieve that..
Upvotes: 0
Views: 932
Reputation: 4577
What about this:
Yes I know this attempt uses a concrete object Person and binding, but using this approach, it is really simple just to modify the columns/properties you need without taking care of the ListView itself. I think that an approach with binding is more simple than any other.
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">
<ListView ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding FirstName}"></GridViewColumn>
<GridViewColumn Header="LastName" DisplayMemberBinding="{Binding LastName}"></GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Window>
C#:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ObservableCollection<Person> list = new ObservableCollection<Person>();
list.Add(new Person() { FirstName = "Firstname", LastName = "Lastname"});
DataContext = list;
}
}
public class Person : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _firstName;
public string FirstName {
get
{
return _firstName;
}
set
{
_firstName = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
}
}
}
private string _lastName;
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("LastName"));
}
}
}
}
Upvotes: 3
Reputation: 5195
In WPF when changing items you almost always do the changes in your bound data and notify the UI about these changes. So you have to utilize ObservableCollection and implement INotifyPropertyChanged.
Please do not try to solve WPF issues like you did in WinForms, you will get stuck at some point as the architecture and the programming style is totally different...
XAML:
<Grid>
<StackPanel>
<ListView x:Name="MyListView">
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Width="140" Header="City" DisplayMemberBinding="{Binding City}"/>
</GridView>
</ListView.View>
</ListView>
<Button Height="50" Click="Button_Click">Click</Button>
</StackPanel>
</Grid>
CodeBehind:
private ObservableCollection<MyData> _data;
public MainWindow()
{
InitializeComponent();
_data = new ObservableCollection<MyData>()
{
new MyData() {City = "City1", Name = "Name1"},
new MyData() {City = "City2", Name = "Name2"},
new MyData() {City = "City3", Name = "Name3"},
};
MyListView.ItemsSource = _data;
}
public class MyData : INotifyPropertyChanged
{
private string _name;
public string Name
{
get { return _name; }
set {
_name = value;
OnPropertyChanged("Name");
}
}
private string _city;
public string City
{
get { return _city; }
set
{
_city = value;
OnPropertyChanged("City");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
_data[1].City = "NewValue";
}
Upvotes: 2