Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding to an expression

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.

like image 501
Jessica Boxer Avatar asked Feb 23 '12 03:02

Jessica Boxer


People also ask

What is binding expression?

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.

How do you use expression 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.

What is it data binding?

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.

Which expression operator would you use to concatenate two strings in an expression binding?

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).


2 Answers

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;
 }
like image 152
Avani Vadera Avatar answered Oct 06 '22 01:10

Avani Vadera


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;
  }
}
like image 25
Stafford Williams Avatar answered Oct 05 '22 23:10

Stafford Williams