Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to add property to Window in xaml
    primarykey
    data
    text
    <p>I'm learning MVVM by write an litle app with Login function. In View layer, I have a LoginWindow with some binding like this:</p> <pre><code>&lt;TextBox x:Name="tbxUsername" Grid.Row="0" Grid.Column="1" Width="150" Height="22" Margin="15,10,5,10" Text="{Binding Path=Username, UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;PasswordBox View:PasswordHelper.Attach="True" View:PasswordHelper.Password="{Binding Path=Password, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" x:Name="pwdPassword" Grid.Row="1" Grid.Column="1" Width="150" Height="22" Margin="15,10,5,10" /&gt; </code></pre> <p>The problem is i want to implement a binding like this:</p> <pre><code>&lt;Window.Authenticated={Binding Path=Authenticated, UpdateSourceTrigger=PropertyChanged, Mode=OneWay, NotifyOnTargetUpdated=True} TargetUpdated="authenticated_TargetUpdated"/&gt; </code></pre> <p>Authenticated is a bool value which will changed in my viewmodel. Is there an way for me?</p> <p>Edit for @lain: Here my LoginWindow.xaml (style and layout removed).</p> <pre><code>&lt;Window x:Class="ATCheck_View.LoginWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:View="clr-namespace:ATCheck_View" xmlns:ViewModel="clr-namespace:ATCheck_ViewModel;assembly=ATCheck_ViewModel" Title="Login" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize" SizeToContent="WidthAndHeight" &gt; &lt;Window.DataContext&gt; &lt;ViewModel:LoginViewModel /&gt; &lt;/Window.DataContext&gt; &lt;Grid&gt; &lt;TextBox x:Name="tbxUsername" Grid.Row="0" Grid.Column="1" Width="150" Height="22" Margin="15,10,5,10" Text="{Binding Path=Username, UpdateSourceTrigger=PropertyChanged, TargetNullValue='atcheck', NotifyOnTargetUpdated=True}"/&gt; &lt;PasswordBox View:PasswordHelper.Attach="True" View:PasswordHelper.Password="{Binding Path=Password, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, TargetNullValue='123456'}" x:Name="pwdPassword" Grid.Row="1" Grid.Column="1" Width="150" Height="22" Margin="15,10,5,10" /&gt; &lt;Button x:Name="btnLogin" Width="65" Height="20" Margin="5,15,10,12" Command="{Binding LoginCommand}" CommandParameter=""&gt; &lt;TextBlock VerticalAlignment="Center"&gt;Login&lt;/TextBlock&gt; &lt;/Button&gt; &lt;Button x:Name="btnCancel" Width="60" Height="20" Margin="5,15,5,12" Click="btnCancel_Click"&gt; &lt;TextBlock VerticalAlignment="Center"&gt;Cancel&lt;/TextBlock&gt; &lt;/Button&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>LoginViewModel:</p> <pre><code>public class LoginViewModel: ViewModelBase { private string _username; private string _password; private bool _authenticated = false; public string Username { get { return _username; } set { _username = value; RaisePropertyChangedEvent("Username"); } } public string Password { get { return _password; } set { _password = value; RaisePropertyChangedEvent("Password"); } } public bool Authenticated { get { return _authenticated; } private set { _authenticated = value; RaisePropertyChangedEvent("Authenticated"); } } public ICommand LoginCommand { get { return new RelayCommand&lt;string&gt;(Login); } } private void Login(string p) { Authenticated = true; Console.WriteLine("Infomation:"); Console.WriteLine(Authenticated); Console.WriteLine(Username); Console.WriteLine(Password); } } </code></pre> <p>ViewModelBase implement INotifyPropertyChanged and RelayCommand that I folow John Smith's article.</p> <p>@nit: I tried as your lead, propdp, rebuild, and type Authenticated folow "Window" tag, but nothing happened when I press commbo Ctrl + Space.</p> <p>What all i want to do is an messagebox that will show when Authenticated change from False to True:</p> <pre><code>private bool _authenticated = false; public bool Authenticated { get { return _authenticated; } set { if (value == true) { MessageBox.Show("Logged!"); } } } public LoginWindow() { InitializeComponent(); LoginViewModel myViewModel = (LoginViewModel)this.DataContext; myViewModel.PropertyChanged += myViewModel_PropertyChanged; } void myViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { if (e.PropertyName == "Authenticated") { Authenticated = ((LoginViewModel)sender).Authenticated; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload