Visual Studio/WPF | CSharp > 속성 > set시 대문자로 설정

운영 환경
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community

ぃ tp // m / 7, f9 / ms / 6, cb6, 0f3c2c6240, 442
계속.

Codename 프로퍼티에 관해서, set시에 대문자로 하는 처리를 넣어 보았다.

참고 : ぃ tp // 이 m / 시라 카와 쇼 / ms / 5f8bd065b1 a 4d2fdc8 0
참고 : htps : // 여기. msd 응. 미 c 로소 ft. 코 m / 우동 ws에서 sk와 p / 5-5f828c에서 # 콘텐 t

방법


  • class Person에게 다음의 변경을 한다
  • private에서 코드 이름 추가
  • public Codename의 get와 set를 구현.

  •     // Name, Race, Codename
        public class Person
        {
            public bool Checked { get; set; }
            public string Name { get; set; }
            public string Race { get; set; }
            private string codename;
            public string Codename {
                get { return codename; }
                set { codename = value.ToUpper(); }
            }
        }
    

    모든 코드



    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; }
            private string codename;
            public string Codename {
                get { return codename; }
                set { codename = value.ToUpper(); }
            }
        }
    
    }
    

    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>
    



    Codename이 대문자가 되었습니다.

    변환 이외에도 수치만 입력 등의 처리를 구현할 수 있을 것 같다.

    좋은 웹페이지 즐겨찾기