-
[WPF] Access view control from viewmodelC#/WPF 2023. 5. 31. 16:19
https://dlsenfl.tistory.com/entry/WPF-control-in-viewModel
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
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 댓글