I have an onscreen numeric keypad to type a PIN. What I want to do is disable the buttons when four digits of PIN are entered. I can certainly do this with code pretty easily, but it seems to me to be the sort of thing that should be done with binding.
Something like:
<Button Style="Whatever" IsEnabled="{Binding ElementName=PinBox ???}"/>
It seems there isn't a way to do that (which to be honest seems rather primitive to me.) So I considered the alternative, which is a plain property on the underlying Window class. But I'm not sure how to bind to it. Do I need to specify the class itself as its own data context, or do I need to extract the PIN string into a View Model?
And subsequently, how do I get the plain property to update the GUI?
I suppose I could defined a view model class and have a dependency property called "ButtonsEnabled" but it seems kind of heavyweight for such a simple problem.
Let me know if I am missing something.
A Binding contains all the information that can be shared across several BindingExpression objects. A BindingExpression is an instance expression that cannot be shared and that contains all the instance information about the Binding.
To use expression binding, you need to enable extended binding syntax via the configuration setting xx-bindingSyntax set to complex . This variant uses one-way binding. This allows the automatic recalculation if the model values change. This variant uses one-time binding, meaning that the value is calculated only once.
Data binding is the process that establishes a connection between the app UI and the data it displays. If the binding has the correct settings and the data provides the proper notifications, when the data changes its value, the elements that are bound to the data reflect changes automatically.
The ampersand symbol is the recommended concatenation operator. It is used to bind a number of string variables together, creating one string from two or more individual strings. Any nonstring variable or expression is converted to a string prior to concatenation (even if Option Strict is on).
You can write a converter which return boolean depending on digits in TextBox
The XAML fo r button would be
<Button Content="Test" IsEnabled="{Binding ElementName=PinBox,Path=Text,Converter={StaticResource DigitsToBoolConverter}}" Grid.Row="1" Height="20" Width="100"></Button>
where PinBox is the textbox name used to enter pin.
The Converter function is
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value.ToString().Length >= 4;
}
Another way using commands:
XAML:
<Button Content="2" Style="Whatever" Command={Binding MyCommand} CommandParamater="2"/>
ViewModel:
public ICommand MyCommand { get; private set; }
public string PinNumber { get; private set; }
public void Init()
{
MyCommand = new RelayCommand(
param => AddPinNumberDigit(param),
param => CanAddPin);
}
private void AddPinNumberDigit(string digit)
{
PinNumber += digit;
}
public bool CanAddPin {
get
{
return PinNumber.Length < 3;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With