locked
Two controls binding the same property RRS feed

  • Question

  • I have two controls. When one is visible, the other one is collapsed. Now I use two properties for binding.

    It works but it takes code space. I want to use one property. Can I ?

    Friday, August 12, 2016 5:54 PM

Answers

  • >>I want just to use one Binding property with one parameter to cover two controls.

    I showed you an example of how to this using styles...please read my previous reply again. If your source property is a bool, the only difference is that use the converter for the first binding:

    <Button x:Name="control1" Content="First control" Background="LightBlue" Visibility="{Binding IsViewVisible, Converter={StaticResource BooleanVisibilityConverter}}"> />
            <Button x:Name="control2" Content="Second control" Background="Yellow">
                <Button.Style>
                    <Style TargetType="Button">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsViewVisible}" Value="True">
                                <Setter Property="Visibility" Value="Collapsed" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>


    Hope that helps.

    Please remember to close your threads by marking all helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by ardmore Tuesday, August 16, 2016 8:16 PM
    Tuesday, August 16, 2016 5:04 PM

All replies

  • >>I want to use one property. Can I ?

    Sure. You could define a Style with a DataTrigger that binds to the single source property for the second control. Please refer to the following sample code:

            <Button x:Name="control1" Content="First control" Background="LightBlue" Visibility="{Binding ControlVisibility}" />
            <Button x:Name="control2" Content="Second control" Background="Yellow">
                <Button.Style>
                    <Style TargetType="Button">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ControlVisibility}" Value="Visible">
                                <Setter Property="Visibility" Value="Collapsed" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
    
            <Button Content="Toggle Visibility" Click="Button_Click"/>
    

    public partial class Window16 : Window, INotifyPropertyChanged
        {
            public Window16()
            {
                InitializeComponent();
                DataContext = this;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                ControlVisibility = ControlVisibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
            }
    
            private Visibility _controlVisibility = Visibility.Visible;
            public Visibility ControlVisibility
            {
                get { return _controlVisibility; }
                set { _controlVisibility = value; NotifyPropertyChanged(); }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Saturday, August 13, 2016 9:07 AM

  • Basically my code likes
    Visibility="{Binding IsViewVisible,Converter={StaticResource BooleanVisibilityConverter}}">
    And in the other control
    Visibility="{Binding IsAnotherVisible,Converter={StaticResource BooleanVisibilityConverter}}">
    I want just to use one Binding property with one parameter to cover two controls.


    Monday, August 15, 2016 2:26 PM
  • As Magnus has it, or you can do the same as you have EXCEPT have a NotBooleanVisibilityConverter for one (of course you write this and make it exactly opposite the original one) if you want to inline it.

    My BooleanToVisibilityConverter also takes in an optional parameter to match with and I use it in all places!


    noorbakhsh حميد نوربخش


    • Edited by noorbakhsh Monday, August 15, 2016 3:47 PM
    Monday, August 15, 2016 3:46 PM
  • >>I want just to use one Binding property with one parameter to cover two controls.

    I showed you an example of how to this using styles...please read my previous reply again. If your source property is a bool, the only difference is that use the converter for the first binding:

    <Button x:Name="control1" Content="First control" Background="LightBlue" Visibility="{Binding IsViewVisible, Converter={StaticResource BooleanVisibilityConverter}}"> />
            <Button x:Name="control2" Content="Second control" Background="Yellow">
                <Button.Style>
                    <Style TargetType="Button">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsViewVisible}" Value="True">
                                <Setter Property="Visibility" Value="Collapsed" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>


    Hope that helps.

    Please remember to close your threads by marking all helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by ardmore Tuesday, August 16, 2016 8:16 PM
    Tuesday, August 16, 2016 5:04 PM