-
[WPF] Access view control from viewmodelC#/WPF 2023. 5. 31. 16:19
https://dlsenfl.tistory.com/entry/WPF-control-in-viewModel
[WPF] ***** control in viewModel
x:Name 지정해주고Loaded 될 때 등등 이벤트에 바인딩해주고 구성요소를 x:Name을 주면 ViewModel 에서 contorl 가져다 쓸 수 있음. 코드 비하인드에서 요딴식으루 안써도 됨. DevExpress Service방식 EX
dlsenfl.tistory.com
public interface IView { void AddTextBoxToGrid(); }
2) Inherit code behind View from your IView and implement IView.AddTextboxToGrid() method
public partial class View: IView { public void AddTextBoxToGrid() { // implement here your custom view logic using standard code behind; } }
3) Add a property of type IView to your VM
public class ViewModel { public IView View { get; set; } }
4) Set View property on VM to an instance of View as IView e.g. in code behind:
public partial class View: IView { public View() { // access you VM by the strategy of your framework or choice - this example is when you store your VM in View's DataContext (DataContext as ViewModel).View = this as IView; } public void AddTextBoxToGrid() { // implement here your custom view logic using standard code behind; } }
5) In your VM call IView.AddTextboxToGrid()
public class ViewModel { public IView View { get; set; } public void AddTextBoxToGrid() { if (View == null) return; View.AddTextBoxToGrid() } }
https://stackoverflow.com/questions/14236222/how-can-i-access-a-control-in-mvvm-model-in-viewmodel
How can i access a control in mvvm model in viewmodel?
I have a WPF Window, and in that window I have a grid. I use M-V-VM model and I want to add a TextBox to the grid dynamically in code(in viewmodel) How can I get access to the grid?
stackoverflow.com
728x90'C# > WPF' 카테고리의 다른 글
[WPF] Find Parent (0) 2023.05.31 [WPF] EventHandler (0) 2023.05.31 [WPF] ReadOnly Property (0) 2023.05.24 [WPF] ObservableCollection Filter (0) 2023.05.19 [WPF] 인터넷 link (0) 2023.05.17 댓글