TextBox binding value int, in order to use it - c#

how to get the value of the textbox and try to use it as int with binding?
<TextBox Text="{Binding SelectedAmount}"/>
I have tried like this, but the value of the binding is 0
public string SelectedAmount
{
get { return _selectedAmount; }
set { _selectedAmount = value; }
}
That is my main class, but the valau of the textbox stay 0, it does´t change
public partial class MainWindow : Window
{
int deposit;
int weeks;
int total;
public MainWindow()
{
InitializeComponent();
this.DataContext = new MyClass();
}
public class MyClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public int _selectedAmount;
public int SelectedAmount
{
get
{
return this._selectedAmount;
}
set
{
if (value != this._selectedAmount)
{
this._selectedAmount = value;
NotifyPropertyChanged();
}
}
}
}
public void BtnCompute_Click_1(object sender, RoutedEventArgs e)
{
MyClass ff = new MyClass();
int cc = ff.SelectedAmount;
deposit = cc;
}
}
}

You can bind Text to int with no effort.
When using bindings, you should either derive the class containing bindable properties from the interface INotifyPropertyChanged or the class DependencyObject. otherwise the binding will show only the default (initial) values.
public class MyClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public int _selectedAmount;
public int SelectedAmount
{
get
{
return this._selectedAmount;
}
set
{
if (value != this._selectedAmount)
{
this._selectedAmount = value;
NotifyPropertyChanged();
}
}
}
}
as in here
or
public class MyClass : DependencyObject
{
/// <summary>
/// Gets or Sets SelectedAmount Dependency Property
/// </summary>
public int SelectedAmount
{
get { return (int)GetValue(SelectedAmountProperty); }
set { SetValue(SelectedAmount Property, value); }
}
public static readonly DependencyProperty SelectedAmountProperty =
DependencyProperty.Register("SelectedAmount ", typeof(int), typeof(MyClass), new PropertyMetadata(0));
}
also do not forget to set the DataContext of your view.
//in view's constructor:
this.DataContext = new MyClass();
or
<UserControl>
<UserControl.DataContext>
<vm:MyClass/>
</UserControl.DataContext>
</UserControl>

Simply use like this,
public void BtnCompute_Click_1(object sender, RoutedEventArgs e)
{
MyClass ff = new MyClass();
int amount;
int.TryParse(ff.SelectedAmount, out amount);
deposit = amount;
}

Related

C# Get an object per index on an infinite list of the same object

I have an object which contain a list of this object.
when the user click on a button I want to add a new object
to one of the object in the list per an index. for example if the index is equal to 2
I want to add an object to the object.object.object(new object()).
how am I trying to get the correct object inside the inifinte object.
any better method then this one?
private void addToCommand(int indexTreeView, ClassForTest.CommadStructure CS)
{
if (_indexTreeView == 0)
{
_OCommandStructure.Add(CS);
}
else if (_indexTreeView==1)
{
_OCommandStructure[_OCommandStructure.Count - 1].SubCommandStructure.Add(CS);
}
else if (_indexTreeView == 2)
{
_OCommandStructure[_OCommandStructure.Count - 1].SubCommandStructure[_OCommandStructure[_OCommandStructure.Count - 1].SubCommandStructure.Count-1].SubCommandStructure.Add(CS);
}
}
public class CommadStructure : INotifyPropertyChanged
{
private string _Controller;;
private ObservableCollection<CommadStructure> _SubCommandStructure;
public string NameOfCommand
{
get => _NameOfCommand;
set
{
_NameOfCommand = value;
NotifyPropertyChanged();
}
}
public ObservableCollection<CommadStructure> SubCommandStructure
{
get => _SubCommandStructure;
set
{
_SubCommandStructure = value;
// NotifyPropertyChanged();
}
}
public ObservableCollection<object> Items
{
get
{
ObservableCollection<object> childNodes = new ObservableCollection<object>();
foreach (var group in this.SubCommandStructure)
childNodes.Add(group);
return childNodes;
}
}
public CommadStructure()
{
NameOfCommand = "";
}
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}

How to bind a property of a XAML view to a public variable in the code behind every time the property changes

I am trying to take a property from a XAML control, specifically the TranslationX property, and store it in a public variable every time the value is changed.
I have tried using data binding by implementing the INotifyPropertyChanged interface and binding the TranslationX property to the public variable from my interface implementation, but had no luck
Essentially, I am needing the TranslationX property of a control to trigger function calls depending on the total displacement, ex. if the control is dragged to -200 in the X direction, it triggers function "Y". I cannot seem to access this translation value in a way that allows me to check if it is above or below a certain value.
I am very new to C# and Xamarin, so any advice is much appreciated.
EDIT:
Here is my current ViewModel class:
public class ReceiptPageViewModel : INotifyPropertyChanged
{
double shift = 0;
public double Shift
{
get => shift;
set
{
if (shift == value)
return;
else
{
shift = value;
OnPropertyChanged(nameof(Shift));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
And here is my syntax for my Binding:
TranslationX="{Binding Shift}"
in your XAML
<SomeElement ... TranslationX="{Binding TransX}" ... />
in your ViewModel
double transX;
public double TransX {
get { return transX; }
set {
transX = value;
if (transX > somethresholdvalue) {
...
}
}
}
Follow the MVVM pattern.
Create a Base View Model with the INotifyPropertyChanged.
Then your custom view model is going to inherit from that base class.
BaseViewModel
public class BaseViewModel : INotifyPropertyChanged
{
bool isBusy = false;
public bool IsBusy
{
get { return isBusy; }
set { SetProperty(ref isBusy, value); }
}
string title = string.Empty;
public string Title
{
get { return title; }
set { SetProperty(ref title, value); }
}
protected bool SetProperty<T>(ref T backingStore, T value,
[CallerMemberName]string propertyName = "",
Action onChanged = null)
{
if (EqualityComparer<T>.Default.Equals(backingStore, value))
return false;
backingStore = value;
onChanged?.Invoke();
OnPropertyChanged(propertyName);
return true;
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
var changed = PropertyChanged;
if (changed == null)
return;
changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
Your custom class:
ReceiptPageViewModel
public class ReceiptPageViewModel : BaseViewModel
{
double shift = 0;
public double Shift
{
get { return shift; }
set { SetProperty(ref shift, value); }
}
}
And in your Xamarin Page set the BindingContext to the ViewModel
(This is an example)
public partial class Page1 : ContentPage
{
private ReceiptPageViewModel viewModel;
public Page1()
{
BindingContext = viewModel = new ReceiptPageViewModel();
InitializeComponent();
}
}
Now you can set the property in the XAML view:
<SomeElement ... TranslationX="{Binding Shift}" ... />
Here you can view a full episode about MVVM Pattern with #JamesMontemagno as host.

UWP User control: collection changed event of a binded dependency property

When I call Generate() the events associated with the ObservableCollection (X) is not fired.
What am I doing wrong?
The code:
MyControl.xaml.cs
public ObservableCollection<double> X
{
get { return (ObservableCollection<double>)GetValue(XProperty); }
set { SetValue(XProperty, value); }
}
public static readonly DependencyProperty XProperty =
DependencyProperty.Register(
"X", typeof(ObservableCollection<double>),
typeof(MyControl),
new PropertyMetadata(
new ObservableCollection<double>(),
new PropertyChangedCallback(OnXChanged)));
private static void OnXChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var control = sender as MyControl;
// DoSomething
}
A XAML random page that uses MyControl:
<local:MyControl
Title="Test"
X="{Binding TestX, Mode=TwoWay}"
/>
That page's .cs
public sealed partial class MainPage : Page
{
public ObservableCollection<double> TestX { get; set; }
private static Random rand_ = new Random();
public MainPage()
{
this.InitializeComponent();
TestX = new ObservableCollection<double>();
}
private void Generate()
{
TestX.Clear();
for (int i = 0; i < 100; ++i)
{
TestX.Add(rand_.Next(1, 100));
}
}
....
}
Please note that I don't see any BindingExpression error in the output window.
Update
I noticed that if do like this in the page, it works:
TestX = new ObservableCollection<double>();
this.MyUserControlInstance.X = TestX;
You're missing two things there:
First:
Make sure you set the DataContext in your constructor:
public MainPage()
{
this.InitializeComponent();
DataContext = this; // Important, but you should use a seperated ViewModel instead
TestX = new ObservableCollection<double>();
}
Second:
Your class is missing the INotifyPropertyChanged implementation, as well as the PropertyChanged call for your TestX property:
private ObservableCollection<double> _testX;
public ObservableCollection<double> TestX
{
get { return _testX; }
set
{
if (value == _testX) return;
_testX = value;
OnPropertyChanged();
}
}
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
Side note: Do MVVM!

Binding TextBox.Text to a Property in a class in ObservableCollection

I have two TextBoxes. I have two ObservableCollections. The ObservableCollection has items of the following type:
public class ChartData : INotifyPropertyChanged
{
DateTime _Name;
double _Value;
#region properties
public DateTime Name
{
get
{
return _Name;
}
set
{
_Name = value;
OnPropertyChanged("Name");
}
}
public double Value
{
get
{
return _Value;
}
set
{
_Value = value;
OnPropertyChanged("Value");
}
}
#endregion
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
I need to bind each of the TextBox.Text to the Value Property in each of the ObservableCollections. The ObservableCollections are DataContext for other controls in the window too. Since I have more than one ObservableCollection, I cannot set the DataContext to the Window.
New data is added to the ObservableCollection using:
ObservableCollection<ChartData>lineSeries1Data = new ObservableCollection<ChartData>();
lineSeries1Data.Add(new ChartData() { Name = DateTime.Now, Value = 0.0 });
When a new Value is added to the Collection, I want the TextBox to show the Value property
You can try something like this if you don't need a "real" binding, but just need to display the Value of the last object which is added (pseudo code):
public string NewItem { get; set+notify; }
ctor(){
myCollection = new ObservableCollection<T>();
myCollection.CollectionChanged += OnMyCollectionChanged;
}
private void OnMyCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
{
if (args.Action == NotifyCollectionChangedAction.Add){
var last = args.NewItems.FirstOrDefault();
if (last == null) return;
NewItem = last.Value;
}
}
//XAML:
<TextBox Text="{Binding NewItem, Mode=OneWay}" />

BindingSource with Generic SubClass in Windows Forms

I'm attempting to do what I considered simple data binding between a BindingSource and a ComboBox. I run into issues when the class I am using as the DataSource of the BindingSource has a property that is an instance of a generic class.
I have the following generic class:
public class GenericClass<T>
{
public T Code { get; set; }
public string Description { get; set; }
public override string ToString()
{
return Description;
}
}
I have a class that has an integer Code:
public class IntegerClass : GenericClass<int>
{
// Nothing unique here, for simple test.
}
I also have the class that is set to the BindingSource's DataSource:
public class ClassBindingClass : INotifyProperty Changed
{
private int _id;
private IntegerClass _choice;
private string _name;
public int Id
{
get { return _id; }
set
{
_id = value;
OnPropertyChanged("Id");
}
}
public IntegerClass Choice
{
get { return _choice; }
set
{
_choice = value;
OnPropertyChanged("Choice");
}
}
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertName));
}
}
On my form I create a collection of IntegerClass and set my combobox's datasource as that collection. (This part works fine, the combo box displays the values appropriately.) Then I set the combobox's SelectedValue Binding to the BindingSource's Choice property updating on OnPropertyChanged.
If I replace IntegerClass with a non-generic class when you select a value in the combo box the BindingSource's Choice property changes the NotifyPropertyChanged event is fired and on my form I can update a label saying "Choice has changed!".
When the IntegerClass is part of the ClassBindingClass this no longer works and instead I cannot navigate out of the combo box and instead get a FormatException.
Is what I want to do possible? Can databinding handle generics?
You mention SelectedValue... but your source (and the bound property) are both IntegerClass - so it isn't a value you want to bind, but the item itself. Unfortunately, there is no ComboBox.SelectedItemChanged so you might need to hack it a bit to get 2-way binding...
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
IntegerClass[] choices = new[] {
new IntegerClass { Code = 123, Description = "a b c"},
new IntegerClass { Code = 456, Description = "d e f"},
new IntegerClass { Code = 789, Description = "g h i"},
};
ComboBox cbo = new TwoWayComboBox();
cbo.DropDownStyle = ComboBoxStyle.DropDownList;
cbo.DataSource = choices;
Form form = new Form();
ClassBindingClass obj = new ClassBindingClass();
cbo.DataBindings.Add("SelectedItem", obj, "Choice", true, DataSourceUpdateMode.OnPropertyChanged);
form.DataBindings.Add("Text", obj, "Choice", true, DataSourceUpdateMode.OnPropertyChanged); // show it
form.Controls.Add(cbo);
Application.Run(form);
}
}
class TwoWayComboBox : ComboBox {
public new object SelectedItem
{
get { return base.SelectedItem; }
set { base.SelectedItem = value; }
}
private static readonly object SelectedItemChangedKey = new object();
public event EventHandler SelectedItemChanged {
add { Events.AddHandler(SelectedItemChangedKey, value);}
remove { Events.RemoveHandler(SelectedItemChangedKey, value);}
}
protected override void OnSelectedIndexChanged(EventArgs e)
{
EventHandler handler = (EventHandler)Events[SelectedItemChangedKey];
if (handler != null) { handler(this, EventArgs.Empty); }
base.OnSelectedIndexChanged(e);
}
}

Categories

Resources