0

If, on a WPF window, you have more than 1 {Binding} for a parameter, is it possible to name them ?

For example for images i have (in the XAML part): Visibility="{Binding}" but also on some textboxes i want to make IsEnabled="{Binding}" Is there some way to name them ? So that in the code in the backside (.cs side) it will pick the right one ?

Thanx in advance.

H.B.
  • 166,899
  • 29
  • 327
  • 400
Dante1986
  • 58,291
  • 13
  • 39
  • 54

1 Answers1

2

You can simply have view model wrapping those properties into single object:

public class ViewModel : INotifyPropertyChanged
{
    private bool imagesVisibility;
    private bool isTextBoxEnabled;

    public event PropertyChangedEventHandler PropertyChanged;

    public bool ImagesVisibility
    {
        get { return this.imagesVisibility; }
        set
        {
            this.imagesVisibility = value;
            this.PropertyChanged(this,
                new PropertyChangedEventArgs("ImagesVisibility"));
        }
    }

    public bool IsTextBoxEnabled
    {
        // ... similar as with ImagesVisibility property implementation
    }
}

Note that you'll also need a boolean to visibility converter, which examples of you can find on StackOverflow (here)... or elsewhere.

Then, you simply set instance of ViewModel to your form data context:

public MyForm()
{
    InitializeComponent();
    DataContext = new ViewModel();
}

You can then do images binding like Visibility="{Binding ImagesVisibility}" and textbox IsEnabled="{Binding IsTextBoxEnabled}".

Community
  • 1
  • 1
k.m
  • 30,794
  • 10
  • 62
  • 86
  • it looks like this will work, will dive into this and let you know if i was able to get it to work :) – Dante1986 Jan 15 '12 at 20:11
  • mmm ok, i think i have it all set up correct now, sho erm.. how do i call it in code now ? :P – Dante1986 Jan 15 '12 at 20:31
  • Aah yes. sorry, im still in the first stages of learning c# and WPF hehe. It works indeed perfect. But what do i use if (for example) i now want to enable all the textboxes again. that i can do with 1 command now right ? – Dante1986 Jan 15 '12 at 20:44
  • @Dante: yep, that can be done with 1 command. All this command would have to do it to change `ViewModel.IsTextBoxEnabled` property to `true`. Then, thanks to `INotifyPropertyChanged` interface changes will be propagated to all bound items (textboxes in this case) and update them accordingly. – k.m Jan 15 '12 at 20:48
  • thats what i thought too, but when i put in 'ViewModel.IsTextBoxEnabled = true' in a method, it returns in the errorbox: An object reference is required for the non-static field,method,or property 'WpfApplication1.ViewModel.IsTextBoxEnabled.get' – Dante1986 Jan 15 '12 at 20:52
  • darn, now that thing throws me a nullreferenceExeption.. and the darn book that i bought learning c# doesnt really explain these wpf stuff hehe. i guess i have some name not matching correct. – Dante1986 Jan 15 '12 at 21:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6740/discussion-between-jimmy-keen-and-dante1986) – k.m Jan 15 '12 at 21:03
  • hehe switching true/false trough button works. only how i can have 1 already set to true on the start, keep trowing a NRE :/ – Dante1986 Jan 15 '12 at 21:40