c# 25

[wpf] c# 코드로 폴더 선택하기 - FolderBroswerDialog와CommonOpenDialog

폴더 선택에는 2가지 방법을 사용할 수 있습니다. Form에서 존재하는 FolderBroswerDialog를 사용하거나 NuGet에서 패키지를 설치하여 CommonOpenDialog를 사용할 수 있습니다. 1. FolderBroswerDialog 1) 참조 추가 - 어셈블리 - System.Windows.Forms 추가 2) 코드 사용 c# System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog(); if(fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK){ folder_tb.Text = fbd.SelectedPath; } 2. CommonOpe..

[wpf] c# 코드로 작업 끝내기

c# 코드로 [작업 관리자]에서 [작업 끝내기] 기능을 사용하면서 선택한 프로그램의 간단한 정보도 볼 수 있도록 만들어 보았습니다. "PROCESS"에 작업을 끝낼 프로그램을 넣으시면 됩니다. "PROCESS".CloseMainWindow(); "PROCESS".Refresh(); "PROCESS".Kill(); 자세한 설명을 보고 싶으신 분은 "더보기"를 클릭해서 봐주시길 바랍니다. 더보기 UI 부분 xaml 사용한 UI 이벤트는 다음과 같습니다. ComboBox DropDownOpened ComboBox를 클릭하여 목록이 펼쳐질 때 현재 실행 중인 프로그램들이 ComboBox의 Item이 됩니다. SelectionChanged ComboBox의 Item이 바뀔 때 새롭게 선택된 프로그램의 정보를 Te..

[wpf] c# 코드로 Style 설정하기 - 2 : 다른 Resource Dictionary 사용

다른 Resource Dictionary 안에 존재하는 다른 Stsyle을 c# 코드로 변경하는 방법입니다. string resourcePath = "/프로젝트이름;component/ResourceDictionary위치.xaml"; ResourceDictionary res = (ResourceDictionary)Application.LoadComponent(new Uri(resourcePath, UriKind.Relative)); Control.Style = (Style)res["ResourceKey"]; 예제를 따라 하시려면 더보기를 클릭하세요. 더보기 Grid 안에 Label과 Button을 한 개씩 준비합니다. xaml 2가지 Resource Dictionary를 준비합니다. ResourceDict..

[wpf] c# 코드로 Style 설정하기 - 1 : 같은 Resource Dictionary 사용

같은 Resource Dictionary 안에 존재하는 다른 Style을 c# 코드로 변경하는 방법입니다. 기본적으로 ResourceDictionary가 필요합니다. ResourceDictionary : Dictionary1.xaml 앞에서 만든 ResourceDictionary를 App.xaml에 등록합니다. App.xaml 그 후 xaml이나 c#에서 사용합니다. c# 코드에서 Application.Current 부분은 사용하지 않으셔도 상관없습니다. xaml c# private void Button_Click(object sender, RoutedEventArgs e){ Style s1 = Application.Current.FindResource("TB_Style1") as Style; Style..

[wpf] c# 코드로 Label 색 변경하기

c# 코드로 색을 변경하는 방법은 지정된 색을 이용하는 방법과 RGB 값을 이용하는 방법이있습니다. 공통 부분 xaml 1. 지정된 색 이용 c# // 글자 색 모음 Color[] color = {Colors.IndianRed, Colors.CadetBlue, Colors.BlueViolet, Colors.Black}; // 글자 색 인덱스 int index = 0; private void Button_Click(object sender, RoutedEventArgs e){ // 라벨 색 변경 L1.Foreground = new SolidColorBrush(color[index++]); // 색상 변경 if (index == color.Length){index = 0;} } 2. RGB 값 이용 RGB ..

[wpf] Button 안의 Button 클릭 이벤트

버튼 안에 버튼을 집어넣을 경우가 가끔 있습니다. 그럴 경우 다음과 같이 Button의 Content를 Grid나 StackPanel 등으로 감싼 뒤 내부에 Button을 넣어주면 됩니다. 이벤트 또한 외부 버튼과 똑같이 사용이 가능합니다. xaml c# private void Button_Click(object sender, RoutedEventArgs e){ Button b = sender as Button; if(b == Btn1){ MessageBox.Show("버튼 1 클릭"); }else if(b == Btn2){ MessageBox.Show("버튼 2 클릭"); } } 위와 같이 설정할 경우 내부 버튼의 이벤트를 동작시키면 외부 버튼의 이벤트도 같이 동작됩니다. 이럴 경우 e.Handled를..

[wpf] Dictionary 합과 정렬

기본 Dictionary 정의 Dictionary dic = new Dictionary(); 1. Dictionary 합(숫자의 합만 가능) Key 합 dic.Sum(x => x.Key); Value 합 dic.Sum(x => x.Key); 2. Dictionary 정렬 오름차순 Key 순으로 정렬 var asc_k = dic.OrderBy(x => x.Key); Value 순으로 정렬 var asc_v = dic.OrderBy(x => x.Value); 내림차순 Key 순으로 정렬 var desc_k = dic.OrderByDescending(x => x.Key); Value 순으로 정렬 var desc_v = dic.OrderByDescending(x => x.Value);

728x90