프로그램 개발/C# 24

[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);

[wpf] ScrollViewer와 Mouse Wheel Event

1. 위아래 Mouse Wheel Event 특별한 설정이 필요 없이 기본적으로 ScrollViewer를 사용하면 됩니다. xaml 코드 2. 좌우 Mouse Wheel Event 좌우 Mouse Wheel Event를 하기 위해서는 HorizontalScrollBarVisibility와 PreviewMouseWheel 이벤트를 설정해 주어야 합니다. xaml 코드 cs 코드 private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e) { ScrollViewer sv = sender as ScrollViewer; if (0 > e.Delta) { sv.LineRight(); }else{ sv.LineLeft(); } e..

728x90