[TIL] C# : December 3, 2021

RE_BROTHER·2021년 12월 3일
0

TIL

목록 보기
37/41
post-thumbnail

Why C#?

새로운 회사에 이직하게 되었다.
자체 서비스를 C#으로 운영하고 있기에 적응해야 한다.
아마 개인적으로 스터디하는 내용을 이 게시물에 계속 추가하지 않을까 싶다.
정확하지 않은 내용이 있다면 따로 표시해놓고 수정할 계획이다.

DataGrid

Grid는 컨트롤들을 담지만 DataGrid는 사용자 정의 가능한 표 형태로 데이터를 표시하는 컨트롤로 행/열에 데이터를 표시하는 유연한 방법을 제공

범위
DataGridTextColumnText 지원
DataGridCheckBoxColumnCheckBox 지원
DataGridTemplateColumn나머지 컨트롤

DataGridColumn에 추가할 수 있는 3가지 Column이다. 많이 사용하는 TextCheckBox Column은 따로 지원하지만, 나머지는 DataGridTemplateColumn을 사용해야 한다.

결론 : Text, CheckBox가 아닌 원하는 컨트롤을 넣고 싶을 때 DataGridTemplateColumn을 사용한다.

[참고] WPF DataGrid 예제 - 오라클자바커뮤니티(OJC.ASIA) // C# 관련된 포스팅이 꽤 있는편이다.
따로 시간내서 하나씩 보면 좋을듯

Question mark

Python, Javascript와는 다르게 변수 타입에 ?를 붙여 사용하는 경우가 많다.

public partical class ExampleModel
{
  private int? a = null; // OK
  private int b = null; // error since b is not nullable
}

위와 같이 C#에서 int?, bool?, DateTime?와 같은 T?의 표현은 .NET의 Nullable<T>와 같은 표현이다. Nullable<T> 구조체는 값을 가지고 있는지 체크한다.
Nullable 타입과 연관되어 자주 사용되는 ?? 연산자는 ?? 앞의 파라미터가 NULL인 경우 연산자 뒤의 값을 할당한다.

double _Sum = 0;
DateTime _Time;
bool? _Selected;

public void CheckInput(int? i, double? d, DateTime? time, bool? selected)
{
    if (i.HasValue && d.HasValue)
        this._Sum = (double)i.Value + (double)d.Value;

    // time valid check
    if (!time.HasValue)
        throw new ArgumentException();
    else
        this._Time = time.Value;

    // if selected is NULL > false
    this._Selected = selected ?? false;
}

Nullable 타입이 필요한 이유 : SQL 서버 테이블에서 NULL을 허용하는 컬림이 있을 때, 테이블의 NULL 속성을 표현하기 위해서

Binding

Binding은 데이터 또는 속성(프로퍼티)을 서로 연결하여 동적으로 변환 및 참조할 수 있도록 한다. 한 엘리먼트의 속성다른 엘리먼트의 속성, 또는 데이터와 연결하는 과정
Binding을 통해 간단하게 서로 값들을 참조하여 동적으로 변경시킬 수 있고, 긴 처리 없이 간단하게 리스트에 DataTable을 표시하는 등 장점이 많기 때문에 WPF 작업시에 Binding은 필수적으로 사용된다.

Binding은 XAML측, Code-behind(C#)측에서 하는 방법이 존재한다.

XAML Binding

<!-- XAML Binding -->
<!-- Controll TextBox to ScrollBar -->
<ScrollBar Name="scrollBar" Orientation="Horizontal" Maximum="100" LargeChange="10"
	SmallChange="1" Value="{Binding ElementName=textbox, Path=Text, Mode=OneWay}"/>
<TextBox Name="textbox" VerticalAlignment="Top"/>

Code-behind Binding

int i = 0;
private void btn0_Click(object sender, RoutedEventArgs e)
{
    i++;
    Binding binding = new Binding();
    binding.Source = slider;   // Slider
    binding.Path = new PropertyPath("Value");   // Property of Slider
    if (i % 5 == 0) binding.Mode = BindingMode.Default;
    if (i % 5 == 1) binding.Mode = BindingMode.OneTime;
    if (i % 5 == 2) binding.Mode = BindingMode.OneWay;
    if (i % 5 == 3) binding.Mode = BindingMode.OneWayToSource;
    if (i % 5 == 4) binding.Mode = BindingMode.TwoWay;
    lbl.SetBinding(ContentProperty, binding);

    lblMode.Content = binding.Mode;
}
 
private void btn1_Click(object sender, RoutedEventArgs e)
{
    if(lbl.Content != null) lbl.Content = (double)lbl.Content + 1;
}

Binding Mode

Mode의미
OneTime최초 1회만 값을 전달
OneWayA를 B에 바인딩했다면, B의 값이 변경되면 A의 값도 변경
OneWayToSourceOneyWay와 반대 / B를 A에 바인딩, A의 값이 변경되면 B의 값도 변경
TwoWay하나가 변경되면 다른 하나도 따라서 변경

[참고] Binding Mode Example (XAML)

[참고] Binding Mode Example (Code-behind)

Reference Site

CHashtag Blog

profile
I hope the All-Rounder Developer & Researcher

0개의 댓글