Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind StackPanel.Visibility to the Visibility property of its children

I'm relatively new to DataBinding and just reading into it. What I want to do is the following:

I have a StackPanel with a number of child controls:

        <StackPanel Orientation="Horizontal">
            <TextBox x:Name="textbox1" Width="100">1</TextBox>
            <TextBox x:Name="textbox2" Width="100">2</TextBox>
            <TextBox x:Name="textbox3" Width="100">3</TextBox>
        </StackPanel>

The visibility property of the textboxes can be changed by code. Now, if all TextBoxes are set to Visibility=Collapsed, i also want StackPanel.Visibility set to Collapsed, but if one or more TextBoxes are shown (Visibility=Visible), StackPanel.Visibility should also be set to Visible.

Can this be achieved with a simple DataBinding or do I have to implement this functionality in C# code?

like image 464
Christian Hubmann Avatar asked Dec 17 '22 09:12

Christian Hubmann


1 Answers

I can not think of a way to do this directly through databinding.

Personally I would have a view model behind the view, and set the views DataContext to the view model.

In the view model I would then have a property telling the view if all the textboxes are collapsed. That property would be set by code. Then bind the stackpanel visibility to that property.

(The property must either be a dependancy property, or the view model must implement INotifyPropertyChanged for the view to automatically update)

like image 190
Kjetil Watnedal Avatar answered Dec 29 '22 10:12

Kjetil Watnedal