WPF C# Command Binding - wpf

I have problem with binding, I want to navigate to new Page (not new window). I Have tried by click event, this is working but if I am trying by ICommand interface, it isn't working good. I don't want why, i have tried in cross-platform, everything works fine. I am thinking what's is wrong with that code.
Main XAML
<Window x:Class="WpfAppFP.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfAppFP"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
Background="#67696d"
x:Name="_CLK">
<Window.Content>
<Border Background="#202021" CornerRadius="40" >
<StackPanel Margin="20">
<Label Content="Type in your data" Foreground="White" HorizontalAlignment="Center" FontSize="20" FontWeight="Bold"/>
<Separator/>
<Label Content="Name" HorizontalAlignment="Center" Foreground="White" FontWeight="Bold"/>
<TextBox HorizontalAlignment="Center" MinWidth="320" MinHeight="32" Foreground="Black" Background="#f5ddff" FontSize="15" FontWeight="DemiBold"/>
<Label Content="Surname" HorizontalAlignment="Center" Foreground="White" FontWeight="Bold"/>
<TextBox HorizontalAlignment="Center" MinWidth="320" MinHeight="32" Foreground="Black" Background="#f5ddff" FontSize="15" FontWeight="DemiBold"/>
<Label Content="Social" HorizontalAlignment="Center" Foreground="White" FontWeight="Bold"/>
<TextBox HorizontalAlignment="Center" MinWidth="320" MinHeight="32" Foreground="Black" Background="#f5ddff" FontSize="15" FontWeight="DemiBold" MaxLength="11" />
<Separator Margin="0,10,0,0"/>
<Button HorizontalAlignment="Center" MinHeight="32" MinWidth="120" Foreground="Black" Background="White" Margin="0,10,0,0" Content="Login" FontWeight="Bold" BorderThickness="2"
x:Name="BtnClk" Command="{Binding CandidatesVM}"/>
</StackPanel>
</Border>
</Window.Content>
MainPage
public partial class MainWindow : Window
{
CandidatesVM candidatesVM;
public MainWindow()
{
InitializeComponent();
candidatesVM = new CandidatesVM();
this.DataContext = candidatesVM;
}
}
CandidatesVM
public class CandidatesVM
{
public NavigationCandidates navigationCandidates { get; set; }
public CandidatesVM()
{
navigationCandidates = new NavigationCandidates(this);
}
public void Navigate()
{
MainWindow mainWindow = new MainWindow();
mainWindow._CLK.Content = new Candidates();
}
}
NavigationCandidates
public class NavigationCandidates : ICommand
{
public CandidatesVM candidatesVM { get; set; }
public event EventHandler CanExecuteChanged;
public NavigationCandidates(CandidatesVM canVM)
{
candidatesVM = canVM;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
candidatesVM.Navigate();
}
}

Bind to navigationCandidates, that's the command:
<Button x:Name="BtnClk" ... Command="{Binding navigationCandidates}"/>
Then I don't know why you are creating a new instance of the MainWindow in the Navigate() method of CandidatesVM. I guess you want to set the Content of the already existing one:
public class CandidatesVM
{
public NavigationCandidates navigationCandidates { get; set; }
private readonly MainWindow _mainWindow;
public CandidatesVM(MainWindow mainWindow)
{
_mainWindow = mainWindow;
navigationCandidates = new NavigationCandidates(this);
}
public void Navigate()
{
_mainWindow.Content = new Candidates();
}
}
public partial class MainWindow : Window
{
CandidatesVM candidatesVM;
public MainWindow()
{
InitializeComponent();
candidatesVM = new CandidatesVM(this);
this.DataContext = candidatesVM;
}
}

You should be binding to navigationCandidates rather than CandidateVM
<Button HorizontalAlignment="Center" MinHeight="32" MinWidth="120" Foreground="Black" Background="White" Margin="0,10,0,0" Content="Login" FontWeight="Bold" BorderThickness="2"
x:Name="BtnClk" Command="{Binding navigationCandidates }"/>
Why ? CandidateVM is served as a data context to your window. So you should be binding to some property of data context and not to data context itself.

Related

Raise usercontrol commands from a parent window using mvvm pattern

My WCF application supports downloading books from a remote service. The user sends a request to the service to download a book, the service gets the request and downloads the book. in response, it sends the download progress for the requested book.
Each element in the BookContainer is a userControl of Book.xaml and its logic represented by bookviewmodel.cs class.
When the user clicks on each book element in the BookContainer.xaml window, the click command inside the bookviewmodel is raised as expected.
I need the help to implement "DownlaodAllCommand" inside containerviewmodel to raise each DownloadCommand of each book.
How should it be implemented using the mvvm pattern.
BookContainer view :
https://onedrive.live.com/redir?resid=3A8F69A0FB413FA4!124&authkey=!ANdfYAk6f0Vf-8s&v=3&ithint=photo%2cpng
code behind:
BookContainer.xaml:
<Window x:Name="BookContainer"
DataContext="{Binding Source={StaticResource Locator}, Path=ContainerViewModel}">
<DockPanel>
<ItemsControl DockPanel.Dock="Top" ItemsSource="{Binding Books}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate >
<Controls:BookControl />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button DockPanel.Dock="Bottom" Command="{Binding DownloadAllCommand}" Content="Download All" > </Button>
</DockPanel>
</Window>
ContainerViewModel
public class ContainerViewModel : ViewModelBase
{
private ObservableCollection<BookViewModel> books;
public ObservableCollection<BookViewModel> Books
{
get
{
if (books == null)
{
// Not yet created.
// Create it.
books = new ObservableCollection<BookViewModel>();
}
return books;
}
}
private ActionCommand _DownloadAllCommand;
public ICommand DownloadAllCommand
{
get
{
if (_DownloadAllCommand == null)
{
_DownloadAllCommand = new ActionCommand(OnDownloadAllCommand, CanDownloadAllCommand);
}
return _DownloadAllCommand;
}
}
private void OnDownloadAllCommand()
{
// help !
}
private bool CanDownloadAllCommand()
{
return true;
}
}
Book.xaml
<UserControl
<Label Grid.Row="0">Title</Label>
<Label Grid.Row="1">Author</Label>
<Label Grid.Row="2">Description</Label>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Title}"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Author}"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Description}"/>
<Button Grid.Column="2" Grid.RowSpan="3" Command="{Binding DownloadCommand}" Content="Download" />
<Ellipse Grid.Column="3"
Height="20" Width="20"
Stroke="Black"
StrokeThickness="0.5"
HorizontalAlignment="Center"
Grid.Row="1"
/>
<Controls:PieSlice Grid.Column="3" Grid.Row="1" Stroke="Black" Fill="Black"
Height="20" Width="20"
StartAngle="0" EndAngle="{Binding Percent}"
HorizontalAlignment="Center" />
</UserControl>
BookViewModel
public class BookViewModel : ViewModelBase
{
public delegate void DownloadRequest(string name);
public event DownloadRequest OnDownalodRequest;
public BookViewModel(BookModel model)
{
this.Book = model;
}
private ActionCommand _DownloadCommand;
public ICommand DownloadCommand
{
get
{
if (_DownloadCommand == null)
{
_DownloadCommand = new ActionCommand(OnDownloadCommand, CanDownloadCommand);
}
return _DownloadCommand;
}
}
protected virtual void OnDownloadCommand()
{
if (OnDownalodRequest != null)
{
OnDownalodRequest.Invoke(this.Author);
}
}
}
BookModel
public class BookModel
{
public string Title { get; set; }
public string Author { get; set; }
public string Description { get; set; }
public Status Status { get; set; }
}
Insid the ContainerViewModel, method OnSaveAllCommand():
Books.ToList().Foreach(book =>
{
if(book.SaveCommand.CanExecute() == false) return;
book.SaveCommand.Execute()
}
);
but take in account that this is a bad practice to use a viewmodel from another viewmodel, try to perform viewmodels communnication on a model level.

WPF displaying object properties from Observable Collection MVVM

I am trying to display the properties from an object which is contained in an observable list but it is just not working.
I was hoping someone could point me in the right direction.
Here is my XAMl
<Window x:Name="MainViewWindow" x:Class="WpfApplication1.View.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local ="clr-namespace:WpfApplication1.ViewModel"
Title="MainView" Height="413.514" Width="607.095">
<Window.DataContext>
<local:MainViewModel />
</Window.DataContext>
<Grid>
<Label Content="Text:" HorizontalAlignment="Left" Margin="55,135,0,0" VerticalAlignment="Top"/>
<Label Content="{Binding Name}" HorizontalAlignment="Left" Margin="98,135,0,0" VerticalAlignment="Top"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="61,108,0,0" VerticalAlignment="Top" Width="75" Command="{Binding UpdateNameCommand}"/>
<Label Content="ActivateButton" HorizontalAlignment="Left" Margin="52,82,0,0" VerticalAlignment="Top"/>
<CheckBox Content="CheckBox" HorizontalAlignment="Left" Margin="173,87,0,0" VerticalAlignment="Top" IsChecked="{Binding ButtonActive}" />
<Label Content="CustomerName" HorizontalAlignment="Left" Margin="55,166,0,0" VerticalAlignment="Top"/>
<StackPanel HorizontalAlignment="Left" Height="100" Margin="173,166,0,0" VerticalAlignment="Top" Width="100"/>
<ItemsControl x:Name="lstPeople" Background="AliceBlue" Width="130" Margin="273,29,196,35" ItemsSource="{Binding CustomerList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
My CustomerList is a property located in the MainViewModel:
ObservableCollection<Customer> _CustomerList;
ObservableCollection<Customer> CustomerList
{
get { return _CustomerList; }
set
{
if (_CustomerList != value)
{
_CustomerList = value;
PropertyChanged(this, new PropertyChangedEventArgs("CustomerList"));
}
}
}
My Customer class only contains one property: Name
public class Customer : INotifyPropertyChanged
{
private string _Name;
public string Name
{
get { return _Name; }
set
{
if (_Name != value)
{
_Name = value;
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
}
I have created an instance of the CustomerList in the MainViewModel constructor but i cannot see any of my customers showing up in the application:
public MainViewModel()
{
ButtonActive = true;
CustomerList = new ObservableCollection<Customer>() { new Customer() { Name = "NewCust33" } };
CustomerList.Add(new Customer() { Name = "NewCust" });
}
It looks like the property you're binding to is private?
You can only bind to public properties.
public ObservableCollection<Customer> CustomerList

Prism 4.1 , Silverllight 5 and .Net 4.5.1 DelegateCommant for Button Not working

I'm new to Prism. I'm trying to setup a test project on Prism. I download Prism 4.1 as I found out Prism 5 doesn't work with Silverlight 5 Yet. so My configuration is Like this.
I've visual studio 2013 , Silverlight 5 and .Net 4.5.1. A basic exercise 1 Homepage divided in to 2 parts with 2 Prism module I followed for Hello world example. Done and working 1 Region Hello, 2end Regigon world
Now in Hello Module I create 1 User form. Created 1 Use.cs with INotifyPropertyChanged etc. Followed MVVM. For created Data Appeared in form. Bow I bind 1 Submit button and display change date on the same region.
I used DelegateCommand. Now working .
No Bug shown But No firing of event.
Project Structure is Like this
.Net Silverlight Navigation Application
aprism
Shell.xaml
Bootstrapper.cs
aprism.web
aprism.Hello
HelloView.xaml
HelloView.xamal.cs : UserControl , IHelloView
IHelloView : IView
IHelloViewModel : IViewModel
HelloViewModel : ViewModelBase, IHelloViewModel
aprism.World
aprism.Business
User.cs : INotifyPropertyChanged , IDataErrorInfo
aprism.Infrastructure
IView
IViewModel
ViewMode
public interface IView
{
IViewModel ViewModel { get; set; }
}
public interface IViewModel
{
IView View { get; set; }
}
public class ViewModelBase :IViewModel ,INotifyPropertyChanged
{
public ViewModelBase(IView view) {
View = view;
View.ViewModel = this;
}
public IView View
{
get;
set;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName) {
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
}
}
}
public class HelloViewModel : ViewModelBase, IHelloViewModel
{
public DelegateCommand SubmitRegistrationForm { get; set; }
public HelloViewModel(View.IHelloView view):base(view)
{
this.View = view;
this.View.ViewModel = this;
this.HelloText = "Prism Hello..";
CreateUse();
//User.PropertyChanged +=User_PropertyChanged;
this.SubmitRegistrationForm = new DelegateCommand(Save, CanSave);
}
/* private void User_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
SubmitRegistrationForm.RaiseCanExecuteChanged();
}*/
private bool CanSave()
{
return true;
}
private void Save()
{
User.DateUpdated = DateTime.Now;
}
#region IHelloViewModel Members
public string HelloText { get; set; }
private User _user;
public User User {
get { return _user; }
set
{
_user = value;
OnPropertyChanged("User");
}
}
private void CreateUse() {
User = new User()
{
Username="Anand",
Email = "akirti.iitk#gmail.com",
Password = "delasoft",
ConfirmPassword = "delasoft"
};
}
/* public Infrastructure.IView View
{
get;
set;
}
*/
#endregion
}
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="Hpmsprism.Hello.View.HelloView"
mc:Ignorable="d"
xmlns:local="clr-namespace:Hpmsprism.Business;assembly=Hpmsprism.Business"
xmlns:Commands="clr-namespace:Microsoft.Practices.Prism.Commands;assembly=Microsoft.Practices.Prism"
d:DesignHeight="500" d:DesignWidth="500">
<UserControl.Resources>
<local:User x:Key="cUser"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Text="{Binding HelloText}" FontSize="26" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="10"/>
<TextBlock HorizontalAlignment="Left" Margin="98,35,0,0"
TextWrapping="Wrap" Text="User Registration"
VerticalAlignment="Top" FontSize="20"/>
<sdk:Label HorizontalAlignment="Left"
Height="28" Margin="110,96,0,0"
VerticalAlignment="Top" Width="90" Content=" User Name : "/>
<sdk:Label HorizontalAlignment="Left"
Height="28" Margin="110,129,0,0"
VerticalAlignment="Top" Width="90" Content=" Email : "/>
<sdk:Label HorizontalAlignment="Left"
Height="28" Margin="110,157,0,0"
VerticalAlignment="Top" Width="90" Content=" Password : "/>
<sdk:Label HorizontalAlignment="Left"
Height="28" Margin="110,204,0,0"
VerticalAlignment="Top" Width="124" Content=" Confirm Password : "/>
<Button Content="Register" HorizontalAlignment="Left" Margin="342,301,0,0"
VerticalAlignment="Top" Width="75" x:Name="SubmitRegisterForm" Commands:Click.Command="{Binding Path=SubmitRegistrationFormCommand}"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="247,92,0,0"
TextWrapping="Wrap" Text="{Binding User.Username,Mode=TwoWay,ValidatesOnDataErrors=True}" VerticalAlignment="Top" Width="170" x:Name="UserName"
/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="247,125,0,0"
TextWrapping="Wrap" Text="{Binding User.Email,Mode=TwoWay,ValidatesOnDataErrors=True}" VerticalAlignment="Top" Width="170" x:Name="Email"/>
<PasswordBox HorizontalAlignment="Left" Margin="247,157,0,0"
VerticalAlignment="Top" Width="170" x:Name="Password"
Password="{Binding User.Password,Mode=TwoWay,ValidatesOnDataErrors=True}"/>
<PasswordBox HorizontalAlignment="Left" Margin="247,200,0,0"
VerticalAlignment="Top" Width="170" x:Name="ConfirmPassword"
Password="{Binding User.ConfirmPassword,Mode=TwoWay,ValidatesOnDataErrors=True}"/>
<sdk:Label HorizontalAlignment="Left" Height="28" Margin="110,257,0,0"
VerticalAlignment="Top" Width="120" Content=" Date Updated :"/>
<TextBlock HorizontalAlignment="Left" Margin="230,257,0,0" TextWrapping="Wrap"
Text="{Binding User.DateUpdated}" VerticalAlignment="Top" x:Name="DateUpdated"/>
</Grid>
</UserControl>
My Button Doesn't fire Submit Command.
PLease Help me.
Thank you
Finally I got It working. After Polishing my MVVM Concept & Unity Again.
Problem is Any Property used in HelloViewModel should have the signature in IHelloViewModel Interface too. And for this Property I Need to Implement INotifyPropertyChanged too.
So I did Like this :
private DelegateCommand _command;
public DelegateCommand SubmitRegistrationFormCommand
{
get { return _command; }
set { _command = value; OnPropertyChanged("SubmitRegistrationFormCommand"); }
}
// This in Implementation
and In Interface I had a Property Signature to Like this
DelegateCommand SubmitRegistrationFormCommand{set;get;}
This may be When Ioc Loads & Map this interface is the role play Thing as DataContext is of Type this ViewModel after Setting the Property
http://social.msdn.microsoft.com/Forums/en-US/531eb18f-3060-4adf-a44e-8dffe0fcbd07/prism-41-silverlight-5-with-net-451-delegatecommand-not-working?forum=wpf
Initially Didn't work for me until I didn't mapped my Interface.

WPF : Passing Custom data to between Custom Control and parent

I am total newbie to WPF. I am creating my custom user control which will display the employee data structure which is as follows I have implemented INotifyPropertyChanged also:
Here's code snippet
public class Employee : INotifyPropertyChanged
{
private int id;
private string name;
private double salary;
private EmployeeDesignation designation;
public int ID
{
get
{
return id;
}
set
{
id = value;
OnPropertyChanged(new PropertyChangedEventArgs("ID"));
//PropertyChanged(ID, new PropertyChangedEventArgs("ID"));
}
}
...
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
}
}
And user control code is as follows :
<UserControl x:Class="Assignment5.EmployeeUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://scenter code herehemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" DataContext="{Binding RelativeSource={RelativeSource Self}, Path=.}"
d:DesignHeight="196" d:DesignWidth="300">
<Grid Height="182">
<Label Content="Name" Height="28" HorizontalAlignment="Left" Margin="26,57,0,0" Name="label1" VerticalAlignment="Top" Width="76" />
<Label Content="Salary" Height="28" HorizontalAlignment="Left" Margin="26,91,0,0" Name="label2" VerticalAlignment="Top" />
<Label Content="ID" Height="28" HorizontalAlignment="Left" Margin="26,23,0,0" Name="label3" VerticalAlignment="Top" />
<Label Content="Designation" Height="28" HorizontalAlignment="Left" Margin="26,125,0,0" Name="label4" VerticalAlignment="Top" />
<TextBox Text="{Binding ElementName=Employee, Path=ID}" Height="23" HorizontalAlignment="Left" Margin="134,28,0,0" Name="IdTextBox" VerticalAlignment="Top" Width="120" />
<TextBox Text="{Binding ElementName=Employee, Path=Name}" Height="23" HorizontalAlignment="Left" Margin="134,59,0,0" Name="NameTextBox" VerticalAlignment="Top" Width="120" />
<TextBox Text="{Binding ElementName=Employee, Path=Salary}" Height="23" HorizontalAlignment="Left" Margin="134,91,0,0" Name="SalaryTextBox" VerticalAlignment="Top" Width="120" LostFocus="SalaryTextBoxLostFocus" />
<TextBox Text="{Binding ElementName=Employee, Path=designation}" Height="23" HorizontalAlignment="Left" Margin="134,125,0,0" Name="DesignationTextBox" VerticalAlignment="Top" Width="120" />
</Grid>
</UserControl>
Here's code behind file
namespace Assignment5
{
public partial class EmployeeUserControl : UserControl, INotifyPropertyChanged
{
public static readonly DependencyProperty EmpDependancyProperty =
DependencyProperty.Register("Employee", typeof(Employee), typeof(EmployeeUserControl)
, new PropertyMetadata(
new PropertyChangedCallback(
EmployeeUserControl.EmployeeDataChanged)));
public Employee Employee
{
get
{
return (Employee)GetValue(EmpDependancyProperty);
}
set
{
SetValue(EmpDependancyProperty, value);
}
}
public EmployeeUserControl()
{
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private void SalaryTextBoxLostFocus(object sender, RoutedEventArgs e)
{
MessageBox.Show(Employee.Name);
}
private static void EmployeeDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
EmployeeUserControl userControl = d as EmployeeUserControl;
userControl.OnPropertyChanged("Employee");
}
}
}
Here's my parent control...
<Window x:Class="Assignment5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:Assignment5"
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=.}">
<Grid>
<my:EmployeeUserControl Employee="{Binding Path=emp, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" HorizontalAlignment="Left" Margin="130,56,0,0" x:Name="employeeUserControl1" VerticalAlignment="Top" Width="285" />
</Grid>
</Window>
and a code behind :
namespace Assignment5
{
public partial class MainWindow : Window
{
public static readonly DependencyProperty EmpDependancyProperty =
DependencyProperty.Register("Employee", typeof(Employee), typeof(EmployeeUserControl)
, new PropertyMetadata(
new PropertyChangedCallback(
EmployeeUserControl.EmployeeDataChanged)));
public Employee Employee
{
get
{
return (Employee)GetValue(EmpDependancyProperty);
}
set
{
SetValue(EmpDependancyProperty, value);
}
}
public MainWindow()
{
emp = new Employee(203,"AAA",23232);
InitializeComponent();
}
}
}
When I run this code. I came to know that Employee object in parent control is getting initialized but in the user control it is not getting initialized. What I am doing wrong here?
Two issues
In your UserControl change the Bindings to
<TextBox Text="{Binding Path=Employee.ID}" Height="23" HorizontalAlignment="Left" Margin="134,28,0,0" Name="IdTextBox" VerticalAlignment="Top" Width="120" />
<TextBox Text="{Binding Employee.Name}" Height="23" HorizontalAlignment="Left" Margin="134,59,0,0" Name="NameTextBox" VerticalAlignment="Top" Width="120" />
<TextBox Text="{Binding Employee.Salary}" Height="23" HorizontalAlignment="Left" Margin="134,91,0,0" Name="SalaryTextBox" VerticalAlignment="Top" Width="120" LostFocus="SalaryTextBoxLostFocus" />
<TextBox Text="{Binding Employee.designation}" Height="23" HorizontalAlignment="Left" Margin="134,125,0,0" Name="DesignationTextBox" VerticalAlignment="Top" Width="120" />
In your Window:
<my:EmployeeUserControl Employee="{Binding Employee, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" HorizontalAlignment="Left" Margin="130,56,0,0" x:Name="employeeUserControl1" VerticalAlignment="Top" Width="285" />
In your constructor emp = new Employee(203,"AAA",23232); does not make any sense. Instead do this.Employee = new Employee(203,"AAA",23232);

TwoWay data binding between various controls

I'm very new to WPF, (I started yesterday) and I'm very confused about data binding. I have a View Model for a Window, which contains a widget called Foo which has its own View Model.
The widget Foo binds its Visibility TwoWay (via a BooleanToVisibilityConverter) to a bool field Visible on its FooViewModel. FooViewModel implements INotifyPropertyChanged and fires a PropertyChanged event whenever Visible is set.
In the Xaml for the Window, it creates a Foo whenever a button is clicked. The Window's view model has another boolean field which is bound TwoWay to the Visibility of its instance of a Foo View. The view model of the WIndow implements INotifyPropertyChanged and fires PropertyChanged events whenever the boolean field is modified.
What I expect this to do is whenever the window's boolean property changes, the visibility of the Foo instance will be set. When this happens I expect the View Model of Foo to be updated, since Foo's visibility binding is two way. When the Foo View Model changes its boolean field I expect the View to change its visibility. Further, I expect the Window view model to be notified that its instance of Foo is no longer visible, and hence the View model of the Window will update its own boolean field. Is this a fundamental misunderstanding?
I post the (obfuscated) code below if it helps shed light on this misunderstanding. Thanks.
Window Xaml
<Window x:Class="XXX.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:XXX.ViewModel"
xmlns:v="clr-namespace:XXX"
Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterOwner" WindowState="Maximized">
<Window.Resources>
<vm:AppViewModel x:Key="AppViewModel"/>
<vm:TwoWayVisibilityConverter x:Key="BoolToVisibility" />
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource AppViewModel}}">
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_Connection" Command="{Binding Authenticate}"/>
<MenuItem Header="_About" Command="{Binding ShowAbout}"/>
<MenuItem Header="_Logout" Command="{Binding Logout}"/>
<MenuItem Header="_Configuration" Command="{Binding Configuration}"/>
<MenuItem Header="_Info" Command="{Binding ShowInfo}"/>
</Menu>
<StackPanel>
</StackPanel>
</DockPanel>
<Border HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="White"
Padding="10"
BorderThickness="0">
<TextBlock Text="XXX"/>
</Border>
<Grid x:Name="Overlay" Panel.ZIndex="1000" DataContext="{Binding Source={StaticResource AppViewModel}}">
<Border HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Visibility="{Binding Path=Modal, Converter={StaticResource BoolToVisibility}, Mode=OneWay}"
Background="DarkGray"
Opacity=".7" />
<v:Configuration HorizontalAlignment="Center"
VerticalAlignment="Center"
Visibility="{Binding Path=ConfigurationVisible, Converter={StaticResource BoolToVisibility}, Mode=TwoWay}"/>
<v:Connect HorizontalAlignment="Center"
VerticalAlignment="Center"
Visibility="{Binding Path=AuthenticateVisible, Converter={StaticResource BoolToVisibility}, Mode=TwoWay}"/>
</Grid>
</Grid>
Window View Model
class AppViewModel : INotifyPropertyChanged
{
[Import(typeof (IEventBus))] private IEventBus _bus;
public AppViewModel()
{
Authenticate = new ForwardCommand(obj => ShowAuthenticationView(), obj => !AuthenticateVisible);
Configuration = new ForwardCommand(obj => ShowConfigurationView(), obj => !ConfigurationVisible);
}
public bool Modal
{
get
{
return AuthenticateVisible || ConfigurationVisible;
}
}
public ICommand Authenticate { get; set; }
public bool AuthenticateVisible { get; set; }
public ICommand ShowInfo { get; set; }
public ICommand ShowAbout { get; set; }
public ICommand Logout { get; set; }
public ICommand Configuration { get; set; }
public bool ConfigurationVisible { get; set; }
private void ShowAuthenticationView()
{
AuthenticateVisible = !AuthenticateVisible;
if (null != PropertyChanged)
{
PropertyChanged(this, new PropertyChangedEventArgs("AuthenticateVisible"));
PropertyChanged(this, new PropertyChangedEventArgs("Modal"));
}
}
private void ShowConfigurationView()
{
ConfigurationVisible = !ConfigurationVisible;
if (null != PropertyChanged)
{
PropertyChanged(this, new PropertyChangedEventArgs("ConfigurationVisible"));
PropertyChanged(this, new PropertyChangedEventArgs("Modal"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
UserControl Xaml
<UserControl x:Class="XXX.Connect"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:XXX.ViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<vm:ConnectViewModel x:Key="ViewModel"/>
<vm:TwoWayVisibilityConverter x:Key="BoolToVisibility" />
</UserControl.Resources>
<Grid Width="280"
Height="173"
DataContext="{Binding Source={StaticResource ViewModel}}"
Visibility="{Binding Path=Visible, Converter={StaticResource BoolToVisibility}, Mode=TwoWay}"
Background="White">
<Label Content="URL" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label1" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="102,12,0,0" Name="url" VerticalAlignment="Top" Width="169" Text="{Binding Path=Url, Mode=OneWayToSource}" TabIndex="0" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="102,70,0,0" Name="username" VerticalAlignment="Top" Width="171" Text="{Binding Path=Username, Mode=OneWayToSource}" TabIndex="2" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="102,41,0,0" Name="password" VerticalAlignment="Top" Width="169" Text="{Binding Path=Password, Mode=OneWayToSource}" TabIndex="1" />
<Label Content="Username" Height="28" HorizontalAlignment="Left" Margin="12,39,0,0" Name="label3" VerticalAlignment="Top" />
<Label Content="Password" Height="28" HorizontalAlignment="Left" Margin="12,68,0,0" Name="label2" VerticalAlignment="Top" />
<DockPanel HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="13">
<Button Content="OK" Height="23" HorizontalAlignment="Left" Margin="5" Name="ok" VerticalAlignment="Top" Width="75" Command="{Binding ConnectCommand}" TabIndex="3" />
<Button Content="Cancel" Height="23" HorizontalAlignment="Left" Margin="5" Name="cancel" VerticalAlignment="Top" Width="75" Command="{Binding CloseCommand}" TabIndex="4" />
</DockPanel>
</Grid>
</UserControl>
UserControl View Model
internal class ConnectViewModel : INotifyPropertyChanged
{
[Import(typeof (IEventBus))] private IEventBus _bus;
public ConnectViewModel()
{
ConnectCommand = new ForwardCommand(obj => Fire(),
obj =>
Visible && !String.IsNullOrEmpty(Url) && !String.IsNullOrEmpty(Url) &&
!String.IsNullOrEmpty(Url));
CloseCommand = new ForwardCommand(obj => Hide(), obj => Visible);
}
public ICommand ConnectCommand { get; set; }
public ICommand CloseCommand { get; set; }
public string Url { get; set; }
public string Username { get; set; }
public string Password { get; set; }
private bool _visible;
public bool Visible
{
get { return _visible; }
set { _visible = value; }
}
private void Fire()
{
_bus.Publish<SessionCreatedEvent, SessionEventHandler>(new SessionCreatedEvent(Url, Username, Password));
Hide();
}
private void Hide()
{
Visible = false;
if (null != PropertyChanged)
PropertyChanged(this, new PropertyChangedEventArgs("Visible"));
}
public event PropertyChangedEventHandler PropertyChanged;
}
Binding always works on properties. So while you Raise Visible in your Hide method, you don't raise it in the actual property. But the property is what the binding engine will set. If you bind this to another dependency property it won't get notified about it.
Btw. whats a TwoWayVisibilityConverter? BooleanToVisibilityConverter is perfectly capable of handling two way bindings.
tl;dr to make two way binding work properly (and in fact even one way binding) you need to implement INotifyPropertyChanged properly which means, if the setter is called raise the property.
public bool Visible
{
get { return _visible; }
set
{
_visible = value;
if (null != PropertyChanged)
PropertyChanged(this, new PropertyChangedEventArgs("Visible"));
}
}

Resources