I am creating a Wpf browser application which has multiple controls over the different pages. Every page follows up the same pattern which is as follows:
At every page load event there are two combo boxes visible to the user. When the user select any value in the combobox
then all the controls like button
, label
and textbox
are visible to the user.
I want to write a common class/function to hide the controls so that I could use in the whole application. Is there any way I can do it?
Solution 1:
In every page you can just add a trigger to Hide/Show the controls like this -
<ComboBox Height="22" Name="comboBox1" />
<Grid>
<Grid.Style>
<Style TargetType="{x:Type Grid}">
<Setter Property="Visibility" Value="Visible"></Setter>
<Style.Triggers>
<DataTrigger
Binding="{Binding ElementName=comboBox1, Path=SelectedItem}"
Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
<Grid.Style>
<!-- Your controls; TextBox, Buttons etc. -->
</Grid>
Solution 2:
In case you want more complex logic(like based on two comboboxes then you can create a MultiValueConverter
and use that in above XAML to set the Visibility
of Grid.
In case of reusing this in multiple windows you can create a Style and apply it to Grid in all the Windows.
Solution 3:
Create a common ViewModel as ethicallogics suggested.
In case you go with ViewModel approch then there is no need of having a Visibility
property, just use the SelectedItem
property in trigger or converter; even if you want to create a seaprate property I would suggest you to make it of Boolean
type and use that in XAML.
You can create a common style in all three cases and reuse that in all windows.
In your ViewModelBase class Create a property(say SelectedItem) of Type as that of Your list To which ComboBox is Binded. And bind this Property to the Selected Item of your ComboBox. Create Another Property (say ControlVisibilty ) of Type Visibility and bind it to all the controls(whose visibility you want to change on the bases of ComboBox selected Item) Visiblity property. Now in the Setter of the SelectedItem property set the value of ControlVisibilty according to your logic.You can do the same for another combobox .I hope this will help to get you an idea.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.