뭐가 문제 일까?

Binding 해서 많은 데이터들은 가져오는게 아닌가? 

 

Nop!  ItemsSource을 사용해서 Binding 해야한다. 

 

.xml 에서 ItemsSource 사용해서 해당 변수들의 상위 grid에서 binding을 추가해줘야한다. 

 

그후 .cs파일에서는 아래와 같이 사용하면 된다. 

public class Binding // 데이터 그리드 바인딩 클래스명
{
    public string Name { get; set; }

    public int Age { get; set; }
}

public ObservableCollection<Binding> BindingList { get; set; } 


public void 생성한 클래스명(){

	BindingList = new ObservableCollection<Binding>();
    
    
    //binding 담는 코드들
    
    BindingList.add();
    
    (.xml 파일에 ItemsSource={Binding BindingList} 한 곳의 x:Name 이름).ItemsSource = BindingList;
    
    
}

 

 

 

 

출처, 참고 자료 : 

https://stackoverflow.com/questions/32257785/bind-the-itemssource-of-a-datagrid-to-a-list

 

Bind the ItemsSource of a DataGrid to a List

I am trying to create a binding between a List and a DataGrid. I haven't found a working Solution in the net which is very strange. In my little example I create a List of objects with the two pu...

stackoverflow.com

 

 

 

갑자기 제대로 된 건데 변수명 인식이 에러 날때

 

솔루션 탐색기 (ctrl + alt + L)

 

프로젝트 우측 클릭 -> 파일 탐색기에서 폴더 열기 (아래에 존재)

obj, bin 파일 삭제하고 다시 빌드하면 된다. 

 

갑자기 이러는 이유에 대해서 생각해 봤는데, 

아마도 .xaml 에서 변수명들을 갑자기 많이 변경해서 얘가 정신이 나가 버린거 아닌가? 라는 예측이 든다.

 

 

둘다 타이머를 설정하는 것. 

화면을 계속해서 갱신(업데이트, 새로고침) 하기 위해서 사용됨.

 

 

 

1. DispatcherTimer

  • WPF (Windows Presentation Foundation) 애플리케이션에서 주로 사용 
  • UI 스레드에서 실행되며, UI 업데이트에 적합 
  • Interval은 TimeSpan 객체로 설정 
  • Tick 이벤트 핸들러는 UI 스레드에서 실행 

 

private DispatcherTimer _Timer;
this._Timer = new DispatcherTimer();
this._Timer.Interval = new TimeSpan(0, 0, 1); // 1초 간격
this._Timer.Tick += _Timer_Tick;
this._Timer.Start();

 

 

 

 

 

2. Timer (System.Windows.Forms.Timer)

 

  • Windows Forms 애플리케이션에서 주로 사용
  • UI 스레드에서 실행되며, 주로 WinForms UI 업데이트에 사용
  • Interval은 밀리초 단위의 정수 값으로 설정
  • Tick 이벤트 핸들러는 UI 스레드에서 실행
Timer timer = new Timer();
timer.Tick += OnTimerTick;
timer.Interval = 10000; // 10초 간격
timer.Start();

 

 

 

1초마다 계속해서 업데이트(화면 갱신)가 되어야하는 화면일 경우에는 

DispatcherTimer 클래스를 사용해서 기능을 만들 수 있다. 

 

나는 popup (버튼 클릭시 나오는 화면)에서 타이머를 설정하고 싶다. 

 

[공식 글]

//DispatcherTimer setup
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
dispatcherTimer.Start();

 

 

 

 

[적용 코드]

public 클래스명()
{
    InitializeComponent();
    this.Unloaded += WdViewStationStatus_Unloaded;
}

private void WdViewStationStatus_Unloaded(object sender, RoutedEventArgs e)
    {
    try
    {
        if (this._Timer != null) this._Timer.Stop();
    }
    catch (Exception ex)
    {
        this._Log.SetHisotry(clsHistory.eHistoryType.Error, ex.ToString());
    }
}   
   
private DispatcherTimer _Timer;
public void StartProcess()
{            
    try
    {
        this.UpdateDisplay();

        this._Timer = new DispatcherTimer();
        this._Timer.Interval = new TimeSpan(0, 0, 1);  //1초마다 
        this._Timer.Tick += _Timer_Tick;
        this._Timer.Start();

        this.Topmost = true;
        this.ShowDialog();
    }
    catch(Exception ex)
    {
        this._Log.SetHisotry(clsHistory.eHistoryType.Error, ex.ToString());
    }
}

private void _Timer_Tick(object? sender, EventArgs e)
{
    try
    {
        this.UpdateDisplay();
    }
    catch (Exception ex)
    {
        this._Log.SetHisotry(clsHistory.eHistoryType.Error, ex.ToString());
    }
}

 

 

this.Unloaded += WdViewStationStatus_Unloaded; 를 해줘야 popup 종료시 타이머가 꺼지는 것이다. 

 

 

 

 

 

출처 : https://learn.microsoft.com/ko-kr/dotnet/api/system.windows.threading.dispatchertimer?view=windowsdesktop-8.0

 

DispatcherTimer 클래스 (System.Windows.Threading)

지정된 시간 간격과 우선 순위로 처리되는 Dispatcher 큐로 통합되는 타이머입니다.

learn.microsoft.com

 

 

스크롤바가 적용이 안되는 이유

 

 

[나의 잘못된 코드]

<StackPanel>
	  <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
      <Grid>
      </Grid>
</StackPanel>

이런식으로 되어있었다.

(chat gpt가 알려줌. ;;)

 

 

 

 

1. 원인 <StackPanel>

<StackPanel> 에서는 ScrollViewr를 사용할 수 없다!

그래서 나는 그냥 안되는 줄알았는데, 

Grid에 Height="" 를 넣어주면 바로 적용된다.. ㅜ.ㅜ!!!

 

아래 사이트에서는 최대 사이즈 MAX를 설정해 줘야한다는데, 그러면 나는 적용이 안되고, 

그냥 사이즈 Height만 지정해줘도 된다. 

https://forum.dotnetdev.kr/t/wpf-scrollviewer/2907/3

 

WPF scrollviewer 질문입니다.

StackPanel의 경우 속한 컨트롤에 따라 Orientation이 Horizontal일 경우 너비, Vertical일 경우 높이가 속한 컨트롤에 맞춥니다. 공유주신 XAML로 짐작컨데 MaxWidth 또는 MaxHeight를 통해서 ScrollViewer의 최대 사

forum.dotnetdev.kr

 

 

 

 

2. 그냥 Grid만 쓰면 자동으로 나옴. 따로 할 것도 없이!

Grid랑 StackPanel 등 레이아웃들이 ScrollViewr를 기본적으로 셋팅 설정 되어있기 때문에 1번의 사례 빼고는 따로 설정해줄 것이 없다고 한다. 

 

 

 

StackPanel 

자식 요소들을 하나씩 쌓는 레이아웃

즉, 이름 그대로 Stack! 스택에 쌓듯이 요소들이 들어간다. 

<StackPanel>
    <Gird>
    </Gird>
    <Gird>
    </Gird>
<StackPanel>

 

예를 들면 

현재 < StackPanel > 안에 <Gird>가 2개 있다. 

그러면 화면에 

<Grid>

<Grid> 

따로 margin을 안줘도 알아서 층 쌓듣이 들어간다. 

 


 

 

Gird 

행과 열로 이루어진 레이아웃을 만드는 데 사용됩니다.

테이블 형식의 배치가 필요할 때 유용합니다.

이거는 쌓는 개념이 아니라. 존재하는 거다. 

<Gird>
    <Gird>
    </Gird>
    <Gird>
    </Gird>
</Gird>

이렇게 있다면, 

화면에 Gird 요소가 겹친다!

그래서 Margin을 사용해서 위치를 조정해줘야 한다. 

 

 


 

 

Gird로 구역 나누는 방법

[ 예시 코드 ]

<Grid Margin="6">       
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="50"/>
    </Grid.RowDefinitions>
    
     <Grid Grid.Row="0" Margin="20,10,0,0">
        <Grid.ColumnDefinitions>
        <ColumnDefinition Width="50*"/>
        <ColumnDefinition Width="10"/>
        <ColumnDefinition Width="50*"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
        
        
    <Grid Grid.Row="1" Margin="20,10,0,0">
        <Grid.ColumnDefinitions>
        <ColumnDefinition Width="50*"/>
        <ColumnDefinition Width="10"/>
        <ColumnDefinition Width="50*"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
</Grid.RowDefinitions>

 

 

1. 행 나누기 

<Grid.RowDefinitions>
    <RowDefinition Height="50"/>
    <RowDefinition Height="58"/>
</Grid.RowDefinitions>

행이 2개로 나눠진 것이다. 

 
 

 

 

2. 행 안에서 열 구분하기.

Grid.Row="0" 를 사용해서 행을 선택할 수 있다. 

<Grid Grid.Row="0" Margin="20,10,0,0">
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="50*"/>
    <ColumnDefinition Width="10"/>
    <ColumnDefinition Width="50*"/>
    <ColumnDefinition/>
</Grid.ColumnDefinitions>

ColumnDefinition을 통해서 열을 구분한다. 

 

 
 

2개의 행 중에서 첫번째 행 안에 3개의 열이 존재하게 된다는 것이다. 

     

 


 

실제 사례

코드와 적용된 것을 보면서 이해할 수 있다. 

아직 *에 대해서는 이해가 안간다. 

*는 가변적 인 비율 기반 크기 조정.

 

찾아보니

- 1. 모든 열의 비율 합계를 계산하고, 

- 2. 그리드의 총 가용 너비를 이 합계로 나눈다.

 

그리드의 전체 공간을 비율에 따라 나누어 각 열이나 행에 할당하기에

다양한 해상도와 창 크기에서 올바르게 동작하도록 할 때 유용하다. 

 

<Grid Margin="6">
    <Grid.RowDefinitions>
        <RowDefinition Height="50*"/>
        <RowDefinition Height="50*"/>
    </Grid.RowDefinitions>

    <Grid Grid.Row="0"  Background="White"  >
    </Grid>

    <Grid Grid.Row="1" Background="SkyBlue">            
    </Grid>
</Grid>

 

<Grid Margin="6">
    <Grid.RowDefinitions>
        <RowDefinition Height="50*"/>
        <RowDefinition Height="50*"/>
    </Grid.RowDefinitions>

    <Grid Grid.Row="0"  Background="White"  >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50*"/>
            <ColumnDefinition Width="10"/>
            <ColumnDefinition Width="50*"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
    </Grid>

    <Grid Grid.Row="1" Background="SkyBlue">            
    </Grid>
</Grid>

 

 

<Grid Margin="6">
    <Grid.RowDefinitions>
        <RowDefinition Height="50*"/>
        <RowDefinition Height="50*"/>
    </Grid.RowDefinitions>

    <Grid Grid.Row="0"  Background="White"  >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50*"/>  <!--0-->
            <ColumnDefinition Width="10*"/>  <!--1-->
            <ColumnDefinition Width="50*"/>  <!--2-->
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="0" Background="Black">
        </Grid>
        <Grid Grid.Column="1" Background="Yellow">
        </Grid>
        <Grid Grid.Column="2" Background="Gray">
        </Grid>
    </Grid>

    <Grid Grid.Row="1" Background="SkyBlue">            
    </Grid>
</Grid>

 

윈도우에서 ctrl + shift 누르면 한글이 변경되는데, 거기서 자간이 달라서 오류가 생겼다!

 

 

INSERT 해야하나??

INSERT INTO [table 명] (column 명) VALUES(0000);


nono! 
이러면 에러 난다. 

 

한 컬럼에 동일한 값을 전부 넣는 쿼리를 작성해줘.

( 이건 동일한 값만 들어간다! )

UPDATE [tabel_name]
SET {column_name} = '0000'

[] 은 유지,  {}은 제거해야함. 

 

 

 

001, 002, 003 이렇게 맵핑 해주는 mssql 쿼리를 작성해줘.
NMA_CLIENT 컬럼에 순차적으로 증가하는 쿼리를 만들어줘. 

 

RowNum AS 함수를 사용해서 할 수 있다!

;WITH CTE AS (
    SELECT
        {매핑할 컬럼명},
        ROW_NUMBER() OVER (ORDER BY {고유키 컬럼명}) AS RowNum
    FROM
        [테이블명]
)
UPDATE CTE
SET {매핑할 컬럼명} = RIGHT('000' + CAST(RowNum AS VARCHAR(3)), 3);

 

+ Recent posts