I get to the point in the WPF application where all the bindings on my controls become pretty repetitive, as well as a bit detailed. Also, if I want to change this binding, I will have to change it in different places, and not just on one.
Is there a way to write the source part of the binding once, for example, to a resource, and then reuse it, referring to it with a more compact syntax. I searched for such opportunities, but did not find it.
What am I doing now
<StackPanel> <ToggleButton x:Name="someToggleButton" /> <Button Visibility="{Binding ElementName=someToggleButton, Path=IsChecked, Converter={StaticResource BoolToVisibilityConverter}}" /> <Grid Visibility="{Binding ElementName=someToggleButton, Path=IsChecked, Converter={StaticResource BoolToVisibilityConverter}}" /> <TextBox Visibility="{Binding ElementName=someToggleButton, Path=IsChecked, Converter={StaticResource BoolToVisibilityConverter}}" /> <CheckBox Visibility="{Binding ElementName=someToggleButton, Path=IsChecked, Converter={StaticResource BoolToVisibilityConverter}}" /> </StackPanel>
What I want to do (Pseudocode)
<StackPanel> <StackPanel.Resources> <Variable x:Name="someToggleButtonIsChecked" Type="{x:Type Visibility}" Value="{Binding ElementName=someToggleButton, Path=IsChecked, Converter={StaticResource BoolToVisibilityConverter}}" /> </StackPanel.Resources> <ToggleButton x:Name="someToggleButton" /> <Button Visibility="{VariableBinding someToggleButtonIsChecked}" /> <Grid Visibility="{VariableBinding someToggleButtonIsChecked}" /> <TextBox Visibility="{VariableBinding someToggleButtonIsChecked}" /> <CheckBox Visibility="{VariableBinding someToggleButtonIsChecked}" /> </StackPanel>
Is there a similar type of similar function or method that will allow me to declare a binding source once and then reuse it?
variables resources wpf binding reusability
jpierson
source share