Visual Studio/WPF > DataGrid > 버튼을 누를 때 TextBox에 입력한 항목 추가
17605 단어 myVisualStudioStudyWPF#migrated
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
ぃ tp // m / 7, f9 / ms / d66f0f76 2f696, 9179
계속.
레코드를 추가하는 처리를 구현.
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
//
using System.IO;
using System.Globalization;
using System.Data;
using System.Collections.ObjectModel;
namespace _170612_t1420_addRecord
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
private ObservableCollection<Person> myList;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
myList = new ObservableCollection<Person>();
myList.Add(new Person { Checked = true, Name = "7of9", Race = "Borg", Codename = "seven" });
myList.Add(new Person { Checked = false, Name = "Janeway", Race = "Human", Codename = "Captain" });
myList.Add(new Person { Checked = true, Name = "Odo", Race = "Unknown", Codename = "Odo" });
this.DataContext = myList;
}
private void dg1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.Column.Header.ToString() == "Checked")
{
//e.Cancel = true; // 非表示
e.Column.IsReadOnly = false;
e.Column.Header = "";
} else
{
e.Column.IsReadOnly = true;
}
}
private void uxDelete_Click(object sender, RoutedEventArgs e)
{
var removeList = myList.Where(x => x.Checked).ToList();
foreach (var item in removeList)
{
myList.Remove(item);
}
}
private void uxAdd_Click(object sender, RoutedEventArgs e)
{
//if (uxName.Text is null ||
// uxRace.Text is null ||
// uxCodename.Text is null)
//{
// return; // error
//}
if (uxName.Text.Length == 0 ||
uxRace.Text.Length == 0 ||
uxCodename.Text.Length == 0) {
return; // error
}
var data = new Person
{
Checked = false,
Name = uxName.Text,
Race = uxRace.Text,
Codename = uxCodename.Text
};
myList.Add(data);
}
}
// Name, Race, Codename
public class Person
{
public bool Checked { get; set; }
public string Name { get; set; }
public string Race { get; set; }
public string Codename { get; set; }
}
}
MainWindow.xaml
<Window x:Class="_170612_t1420_addRecord.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:_170612_t1420_addRecord"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<StackPanel Margin="5">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Name"/>
<TextBox x:Name="uxName" Width="100"/>
<TextBlock Text="Race"/>
<TextBox x:Name="uxRace" Width="100"/>
<TextBlock Text="Codename"/>
<TextBox x:Name="uxCodename" Width="100"/>
<Button x:Name="uxAdd" Content="Add" Width="70"
Click="uxAdd_Click"/>
</StackPanel>
<Button x:Name="uxDelete" Content="delete"
Click="uxDelete_Click" Width="100"/>
<DataGrid Height="300" x:Name="dg1"
AutoGenerateColumns="True"
IsReadOnly="False"
CanUserAddRows="False"
ItemsSource="{Binding}" AutoGeneratingColumn="dg1_AutoGeneratingColumn"/>
</StackPanel>
</Grid>
</Window>
초기 화면
Neelix 추가 후
Odo는 Voyager의 일원이 아니었다. . .
Reference
이 문제에 관하여(Visual Studio/WPF > DataGrid > 버튼을 누를 때 TextBox에 입력한 항목 추가), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/7of9/items/6ecb6a0f3c2c6240a442텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)