madaxe2020
Well-known member
- Joined
- Sep 7, 2020
- Messages
- 50
- Programming Experience
- 5-10
The behavior I wont is for the textbox to clear after the button is selected and the record added to the combobox and the datagridview
I'm using MVVM as my pattern, and have bound the textbox to a property which I'm setting to string.Empty after the new record is created, but the textbox wont update.
Thanks
Madaxe
I'm using MVVM as my pattern, and have bound the textbox to a property which I'm setting to string.Empty after the new record is created, but the textbox wont update.
Thanks
Madaxe
XAML:
<Window x:Class="WPFBinding.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:WPFBinding"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="530">
<Grid>
<StackPanel Width="500">
<TextBox x:Name="TextInput" Text="{Binding myContinentName,
Mode=TwoWay,
NotifyOnSourceUpdated=True,
UpdateSourceTrigger=PropertyChanged}"/>
<Button Command="{Binding ButtonCommand}"
CommandParameter="{Binding ElementName=TextInput,
Path=Text}"
Content="Add to ComboBox"/>
<ComboBox ItemsSource="{Binding myMessages}"/>
<DataGrid x:Name="DGD_UserList"
AutoGenerateColumns="False"
ItemsSource="{Binding myContinents,
Mode=TwoWay,
NotifyOnSourceUpdated=True,
UpdateSourceTrigger=PropertyChanged}"
Margin="0,5,5,5"
ColumnWidth="*"
Background="#FF9F9E9E" HorizontalAlignment="Right" Width="481">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding continent_name}" Header="Continent Name" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding modifying_user_id}" Header="Modifying User" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding modifying_date}" Header="Modification Date" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Grid>
</Window>
Relay:
using System;
using System.Windows.Input;
namespace WPFBinding
{
public class RelayCommand : ICommand
{
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
{
throw new NullReferenceException("execute");
}
else
{
this._execute = execute;
this._canExecute = canExecute;
}
}
public RelayCommand(Action<object> execute) : this(execute, null)
{
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
return this._canExecute == null ? true : this._canExecute(parameter);
}
public void Execute(object parameter)
{
this._execute.Invoke(parameter);
}
}
}
View Model Base:
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace WPFBinding.Models.Base
{
public class ViewModelBase : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
View Model:
using System;
using System.Collections.ObjectModel;
using WPFBinding.Models;
using WPFBinding.Models.Base;
namespace WPFBinding.ViewModels
{
public class DataViewModel : ViewModelBase
{
public string myContinentName { get; set; }
public ObservableCollection<Continent> myContinents { get; set; }
public ObservableCollection<string> myMessages { get; private set; }
public RelayCommand ButtonCommand { get; private set; }
public DataViewModel()
{
this.myMessages = new ObservableCollection<string>() { };
this.myContinents = new ObservableCollection<Continent>() { };
this.ButtonCommand = new RelayCommand(AddtoComboBox, ComboboxCanAdd);
}
public void AddtoComboBox(object message)
{
if (this.myMessages.Contains((string)message) == false)
{
Continent continent = new Continent((string)message, 1, DateTime.Now);
this.myContinents.Add(continent);
this.myMessages.Add((string)message);
this.myContinentName = string.Empty;
}
}
public bool ComboboxCanAdd(object message)
{
if (string.IsNullOrWhiteSpace((string)message) == true || this.myMessages.Contains((string)message) == true)
{
return false;
}
else
{
return true;
}
}
}
}
Model:
using System;
namespace WPFBinding.Models
{
public class Continent
{
public string continent_name { get; set; }
public int modifying_user_id { get; set; }
public DateTime modifying_date { get; set; }
public Continent(string ContinentName, int ModifyingUserId, DateTime ModifyingDate)
{
this.continent_name = ContinentName;
this.modifying_user_id = ModifyingUserId;
this.modifying_date = ModifyingDate;
}
}
}