c# - How to bind DependencyProperty without using MVVM -
Well, I am doing a small project and I came to know that it was not necessary to implement the complete MVVM.
I am trying to tie some properties behind the code, but can not manage it to work.
The use of dot dependency properties and the code behind it is binding.
I tried to follow these links and questions:
But they MVVM related or at least I can not adapt code in my case.
The illustration should be very simple.
MainWindow.xaml
& lt; Label name = "_lblCurrentPath" style = "{StaticResource CustomPathLabel}" tooltip = "{Binding CurrentPath}" content = "{binding currentpath, mode = twofold, updated SourceTrigger = property changes}" />
MainWindow.xaml.cs
Public main window () {InitializeComponent (); SetBindings (); } #region Properties public static read-only dependency property current page authenticity = dependency Registrar ("current", typef (string), typef (menwindows), new property matadata (string. Api, oncantpath changed); Public string current path {return (gate) gateway (current pathprote); } Set {set value (current path, value); }} #endrian area # binding private zero setbinding () {// label current path binding _currentPath = new binding ("current"); _currentPath.Source = CurrentPath; This._lblCurrentPath.SetBinding (Label.ContentProperty, _currentPath); } #endregion #region Ways Private Refresh Refresh () {MessageBox.Show ("Refresh!"); } Private string search () {WinForms.FolderBrowserDialog dialog = new WinForms.FolderBrowserDialog (); WinForms.DialogResult _dResult = dialog.ShowDialog (); Switch (_dResult) {Case WinForms.DialogResult.OK: CurrentPath = Dialog. Chosen path; break; Default: Break; } Return CurrentPath; } #partner # Regional developments by changing private static zero interval lesson (Dependency Object D, Dependency Property Entrepreneur E) {Main Window Example = Main Menu; Instance.Refresh (); } Public Zero OpenSearchEclipsePath (Object Sender, RoutedEventArgs E) {CurrentPath = Search (); } Public Zero RefreshEclipsePath (Object Sender, RoutedEventArgs e) {Refresh (); }
Any ideas?
If this is a bad practice and I should use MVVM comments, then we are welcome.
Apart from this ... In this case related to property, where do I want to use an MVVM approach, is it better to register these incidents? I found a little tedious experiment of custom command binding.
First of all, you can completely use binding MVVM. I do not tell it, because the code is very clean when you use MVVM, but it can be . You just have to put this line in your constructor:
this.DataContext = this;
Your view is now your visual model! Like I said, this is not a good idea.
Now, you have the dependency property
in your main window
category. Do not do that This is not entirely objective. The DPs are there, so the guardian control can give them binding. main window
has no parent; That's why a DP is useless.
You only need to set up a regular property:
public string current path {get current return; } Set {currentPath = value; NotifyPropertyChanged (); }}
and then apply MainWindow
INotifyPropertyChanged
(I mentioned that using this simple visual model Does it make more sense?).
To answer your order
question, if you are opposed to using the command, then simply register for events, however, command < / Code> The URL is a great way to click in the visual model without breaking the MVVM. The syntax is not is bad if you are going to look "View as a model" approach anyway,
command
does not buy you much.
Comments
Post a Comment