0
나는이 개 분야listView를 새 항목으로 업데이트하려면 어떻게해야합니까?
string Name
및
int Age
가 어떻게 사용자가 Button
를 누를 때 listView
에 바인딩 된 컬렉션에이 정보를 추가 할 있나요?
나는이 개 분야listView를 새 항목으로 업데이트하려면 어떻게해야합니까?
string Name
및
int Age
가 어떻게 사용자가 Button
를 누를 때 listView
에 바인딩 된 컬렉션에이 정보를 추가 할 있나요?
다음은 간단한 예입니다. this 문서에 설명 된 RelayCommand 및 ObservableObject 클래스를 사용한다고 가정합니다.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60*" />
<RowDefinition Height="20*" />
<RowDefinition Height="20*" />
</Grid.RowDefinitions>
<ListBox ItemsSource="{Binding People}"
Margin="5">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<TextBlock Text=", " />
<TextBlock Text="{Binding Age}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="50*" />
<RowDefinition Height="50*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label>Name:</Label>
<Label Grid.Row="1">Age:</Label>
<TextBox Grid.Column="1"
Margin="5"
Text="{Binding Name}"/>
<TextBox Grid.Column="1"
Grid.Row="1"
Margin="5"
Text="{Binding Age}"/>
</Grid>
<Button Grid.Row="2"
Margin="5"
Command="{Binding AddPersonCommand}">Add Person</Button>
</Grid>
다음
MainWindow.xaml
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
마지막으로 정보를 조작하기위한 간단한 viewModel을 만듭니다.
public class AppViewModel : ObservableObject
{
private int _age;
private string _name;
private RelayCommand _AddPersonCommand;
private ObservableCollection<Person> _people = new ObservableCollection<Person>()
{
new Person { Name="Ben", Age=26 },
new Person { Name="George", Age=40 },
new Person { Name="Amy", Age=38 },
new Person { Name="April", Age=22 },
new Person { Name="Mick", Age=45 },
new Person { Name="Steph", Age=32 },
new Person { Name="Mel", Age=35 },
new Person { Name="Emma", Age=28 },
};
public IEnumerable People
{
get { return _people; }
}
public ICommand AddPersonCommand
{
get
{
if (_AddPersonCommand == null)
{
_AddPersonCommand = new RelayCommand(p=> this.AddPerson());
}
return _AddPersonCommand;
}
}
public string Name
{
get { return _name; }
set
{
_name = value;
NotifyPropertyChanged("Name");
}
}
public int Age
{
get { return _age; }
set
{
_age = value;
NotifyPropertyChanged("Age");
}
}
private void AddPerson()
{
Person p = new Person();
p.Name = _name;
p.Age = _age;
_people.Add(p);
}
}
일부 코드가 필요합니다. MVVM 프레임 워크를 사용하고 있습니까? 지원 INotify ...? – tzerb
표시 할 항목이 없습니다. 두 필드가있는 간단한 ListView가 있고 viewmodel에 새 ListViewItem을 추가하려고합니다. – Yanshof