I want to implement that when a textbox value is changed, my Add button become available.
I bind the textbox with viewModel:
<TextBox Name="nameTbx" Text="{Binding Path=NewNode.Name, Mode=TwoWay}" />
My button:
<Button Content="Add" Command="{Binding Path=AddNewNodeProperty}"/>
In the XAML code behind i set the DataContext to my ViewModel. ViewModel looks like:
/* code*/
private Node _newNode = new Node();
public Node NewNode
{
get
{
return _newNode;
}
set
{
_newNode = value;
OnPropertyChanged("NewNode");
}
}
private AddNode _addNewNodeProperty;
public AddNode AddNewNodeProperty
{
get
{
return _addNewNodeProperty;
}
}
In a constructor i initialize the _addNewNodeProperty
this._addNewNodeProperty = new AddNode(this);
Here is my AddNode class:
public class AddNode : ICommand
{
private ServiceMapViewModel viewModel;
public AddNode(ServiceMapViewModel viewModel)
{
this.viewModel = viewModel;
this.viewModel.PropertyChanged += (s, e) =>
{
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, new EventArgs());
}
};
}
public bool CanExecute(object parameter)
{
bool b = !string.IsNullOrWhiteSpace(this.viewModel.NewNode.Name);
return b;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
this.viewModel.AddNewNode();
}
}
And finally my Node class:
public class Node
{
public string Name { get; set; }
public bool? IsChecked { get; set; }
public Group Group { get; set; }
public Category Category { get; set; }
public string Metadata { get; set; }
public List<string> Children = new List<string>();
public List<string> Parents = new List<string>();
}
The problem is that when i change my textbox text, NewNode is getting the value for me, but it supposed to set .
Tnx in advanced!
EDIT
Let me add something:
I also have a datagrid on the screen and when Selected Item is changed, my Add butom become available.
Selected Item:
<DataGrid Name="nodeDataGrid" ItemsSource="{Binding Path=MyServiceMap.Nodes}"
Background="Silver" Margin="0,34,10,10" IsReadOnly="True" SelectedItem="{Binding Path=SelectedNode}" >
And VM:
private Node _selectedNode = new Node();
public Node SelectedNode
{
get
{
return _selectedNode;
}
set
{
_selectedNode = value;
OnPropertyChanged("SelectedNode");
}
}
Your TextBox is bound to the
Nameproperty of yourNodeobject. TheNameproperty does not raise aPropertyChangedevent when it changes. Only the entireNewNodeproperty will raise the event when it is changed.Edit to offer a suggestion
One approach would be to create a
NodeViewModelclass which, as mtaboy suggested, would implement theINotifyPropertyChangedinterface. You would create whatever properties on the ViewModel that you would want to expose to your user. You can think of the ViewModel as sitting between your Model (i.e.,Nodeclass) and your View (the UI shown to the user).One change I would recommend would be to encapsulate the
ICommandlogic into its own class. The one I often use is theRelayCommandclass which can be found in Josh Smith’s MVVM article on MSDN. If you use that class, you can define either a property or a private method in your ViewModel class which would return whether the user could Add or not. For example,When instantiating your
RelayCommand, you can pass a lambda for the second parameter that refers to that method above.You would expose an
ICommandproperty from your ViewModel which would return theRelayCommandobject.Hopefully, that gets you started.