WPF TextBlock에 다중 바인딩으로 글자 표현하는 방법
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | xmlns:p="clr-namespace:CLM.Properties" <Button VerticalAlignment="Top" Grid.Column="1" Style="{StaticResource ResourceKey=roundbutton}" Command="{Binding EVCommand_PolicyManageWindow}"> <ItemsControl Style="{StaticResource ResourceKey=CustomGrid}" > <Image Grid.Row="0" Source="../Resource/Images/Icons/MenuIcon/02.png" Style="{StaticResource ResourceKey=CustomImage}" /> <TextBlock Grid.Row="1" Style="{StaticResource ResourceKey=CustomTextBlock}" > <TextBlock.Text> <MultiBinding StringFormat="{}{0} {1}"> <Binding Source="{x:Static p:Resources.정책}" /> <Binding Source="{x:Static p:Resources.관리}" /> </MultiBinding> </TextBlock.Text> </TextBlock> </ItemsControl> </Button> | cs |
'Code > C#' 카테고리의 다른 글
WPF Combobox 선택 이벤트, old 값이 나올때 (0) | 2019.03.19 |
---|---|
WPF C#으로 UserControll을 바로 다이얼로그 화면으로 출력 하고 싶을때 (0) | 2019.03.12 |
WPF XAML로 다른 클래스 라이브러리에 있는 xaml 참조하는 방법 (0) | 2019.03.12 |
Mvvm이란 무엇인가 (0) | 2019.02.28 |
[WPF] DelegateCommand 사용법 (0) | 2019.02.20 |
Mvvm이란 무엇인가
(출처 : https://happybono.wordpress.com/2017/10/10/mvvm-i-mvvm-%ED%8C%A8%ED%84%B4%EC%97%90-%EB%8C%80%ED%95%9C-%EA%B8%B0%EB%B3%B8-%EA%B0%9C%EB%85%90/)
- 설명 -
Mvvm이란 Model, View, ViewModel을 줄인 약자로써 MVC 패턴에서 단점으로 여겨지는 View와 Model의 의존성을 최소화 하기 위해 사용되어진다.
간단히 말하자면 화면에 보이는 View 와 프로그래밍적 로직을 담고 있는 Model을 서로 분리해서, Model을 기준으로 다른 View로 표현하게 할 수 있고, View를 기준으로 다른 데이터를 표현하기 위해 Model을 바꾸는 것이 쉬운 구조로 만드는 것이 주 목적이다.
- 주요 기능-
이러한 Mvvm을 구현하기 위해서 만들어진것이. Command 기능과 데이터 바인딩이라는 기법이다.
Command는 View에서 UI 이벤트를 처리하기 위해서 만들어졌다.
Command를 이용하여 View에서는 Model이 변경될 때 UI에 붙은 이벤트 이름을 변경할 필요가 없어졌다.
데이터 바인딩은 Model에서 만들어진 Data를 View에 표현하기 위해서 만들어졌다.
데이터 바인딩을 이용하여 Model에서 다른 View로 데이터를 표현할때 View의 UI를 호출하지 않고 데이터를 출력 할 수 있게 되었다.
- 구성 -
기본적으로 UI가 나오는 프로그램의 기본 요소는
자료를 저장할 데이터 ,
데이터를 처리하는 로직 ,
데이터를 표현하는 UI
이렇게 3가지 정도로 나눌 수 있다.
여기에 Mvvm을 하기 위해 Property - 데이터 바인딩용 과 Delegate-Command용이 추가된다.
이러한 요소들을 Model, ViewModel, View로 나누어 넣으면
Model
- 데이터를 처리하는 메소드
ViewModel
- 데이터를 저장하고 있는 프로퍼티
- 데이터를 저장하기 위한 공간
- 이벤트 처리를 위한 Delegate
View
- Command가 붙은 UI
이렇게 구성이 된다.
- 도움이 되는 프레임워크 또는 라이브러리 -
WPF
- DevExpress
- Prism
- 도움이 되는 예제 -
https://github.com/2Bbear/WindowsProgrmaDevelop/tree/master/WPF
'Code > C#' 카테고리의 다른 글
WPF Combobox 선택 이벤트, old 값이 나올때 (0) | 2019.03.19 |
---|---|
WPF C#으로 UserControll을 바로 다이얼로그 화면으로 출력 하고 싶을때 (0) | 2019.03.12 |
WPF XAML로 다른 클래스 라이브러리에 있는 xaml 참조하는 방법 (0) | 2019.03.12 |
WPF TextBlock에 다중 바인딩으로 글자 표현하는 방법 (0) | 2019.03.11 |
[WPF] DelegateCommand 사용법 (0) | 2019.02.20 |
[WPF] DelegateCommand 사용법
정보 참고는
https://illef.tistory.com/entry/WPF-Command-Part4-DelegateCommand
http://www.sysnet.pe.kr/2/0/10917
이곳에서 했습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace HowToDelegateCommand { class DelegateCommand<T> : ICommand { Action<T> executeTargets = delegate { }; Func<bool> canExecuteTargets = delegate { return false; }; bool m_Enabled = false; public bool CanExecute(object parameter) { Delegate[] targets = canExecuteTargets.GetInvocationList(); foreach(Func<bool> target in targets) { m_Enabled = false; bool localenable = target.Invoke(); if(localenable) { m_Enabled = true; break; } } return m_Enabled; } public void Execute(object parameter) { if (CanExecute(parameter)) { executeTargets(parameter !=null?(T)parameter: default(T)); } } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public event Action<T> ExecuteTargets { add { executeTargets += value; } remove { executeTargets -= value; } } public event Func<bool> CanExecuteTargets { add { canExecuteTargets += value; } remove { canExecuteTargets -= value; } } } } | cs |
Generic을 이용한 Command 구현입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #region 어셈블리 System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 // C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.dll #endregion using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows.Markup; namespace System.Windows.Input { // // 요약: // 명령을 정의합니다. [TypeConverter("System.Windows.Input.CommandConverter, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")] [TypeForwardedFrom("PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")] [ValueSerializer("System.Windows.Input.CommandValueSerializer, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")] public interface ICommand { // // 요약: // 명령을 실행해야 하는지 여부에 영향을 주는 변경이 발생할 때 발생합니다. event EventHandler CanExecuteChanged; // // 요약: // 명령을 현재 상태에서 실행할 수 있는지를 결정하는 메서드를 정의합니다. // // 매개 변수: // parameter: // 명령에 사용된 데이터입니다. 명령에서 데이터를 전달할 필요가 없으면 이 개체를 null로 설정할 수 있습니다. // // 반환 값: // 이 명령을 실행할 수 있으면 true이고, 그러지 않으면 false입니다. bool CanExecute(object parameter); // // 요약: // 명령이 호출될 때 호출될 메서드를 정의합니다. // // 매개 변수: // parameter: // 명령에 사용된 데이터입니다. 명령에서 데이터를 전달할 필요가 없으면 이 개체를 null로 설정할 수 있습니다. void Execute(object parameter); } } | cs |
위 코드에서 사용한 ICommand는 이렇게 구성이 되어 있습니다.
CanExecute의 경우 실행이 가능하다면 return을 하는 형식으로 구현 할 수 있습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class RelayCommand : ICommand { public event EventHandler CanExecuteChanged; public bool CanExecute(object parameter) { return DateTime.Now.Hour != 9; } public void Execute(object parameter) { Console.WriteLine(DateTime.Now + ": RelayCommand.Execute - " + parameter); } } |
이렇게 말입니다.
위 코드는 현재 시간이 9시가 아닐 경우 return을 하는 형태입니다.
즉 9시가 아닌 경우에만 동작을 하도록 하고 싶을 경우 이렇게 작성하게 됩니다.
1 2 3 4 | if (Command.CanExecute(null) == true) { Command.Execute(txt); } | cs |
그리고 위 커맨드를 다른 곳에서 호출 할때 이런 식으로 호출하게 됩니다.
즉 CanExecute가 반환된 값이 있을때 Command.Excute를 한다고 말이죠.
=====================================================================
그러한 맥락으로 보았을 때 DelegateCommand는
어디선가 호출자가 Command를 호출할때, 즉 어떤 호출자가 CanCommand를 실행하였을 때,
해당 호출자가 CanCommand가 갖고 있는 Target들, 즉 호출을 할 수 있는 호출자 목록 안에 있을 때 해당 호출자에 맞추어 Execute를 실행하게 해주는 코드를 갖고있습니다.
따라서 CanCommand 에는 foreach 문으로 자신에게 할당 되어 있는 호출자들을 매번 검색해야합니다.
순서는 execute -> CanExecute의 순으로서. CanExecute에서 "이 호출자는 실행해도 되는 호출자야"라고 허락을 해야 execute가 실행하게 되는 구조입니다.
'Code > C#' 카테고리의 다른 글
WPF Combobox 선택 이벤트, old 값이 나올때 (0) | 2019.03.19 |
---|---|
WPF C#으로 UserControll을 바로 다이얼로그 화면으로 출력 하고 싶을때 (0) | 2019.03.12 |
WPF XAML로 다른 클래스 라이브러리에 있는 xaml 참조하는 방법 (0) | 2019.03.12 |
WPF TextBlock에 다중 바인딩으로 글자 표현하는 방법 (0) | 2019.03.11 |
Mvvm이란 무엇인가 (0) | 2019.02.28 |
보호되어 있는 글입니다.
내용을 보시려면 비밀번호를 입력하세요.
보호되어 있는 글입니다.
내용을 보시려면 비밀번호를 입력하세요.