It is necessary to make the button visible only if the element with index 1 is selected in TabControl. I try to do it through the converter, but it does not work.

<Button.Visibility> <Binding ElementName="mainTab" Converter="{StaticResource BackButtonVisibleConverter}" UpdateSourceTrigger="PropertyChanged"> </Binding> </Button.Visibility> 

Converter

 public class BackButtonVisibleConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { TabControl tab = value as TabControl; if (tab.SelectedIndex == 1) return Visibility.Visible; return Visibility.Hidden; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } 
  • return Visibility.Visible; ELSE return Visibility.Hidden; - MaximK
  • one
    @MaksimKutovoy, why else? If the first return works, we will not reach the second. - Lightness
  • and in XAML resources you create BackButtonVisibleConverter ? - Dmitry Chistik
  • @ Dmitry Chistik yes, I create. The converter only works once for some reason, when changing TabControl.SelectedIndex, it no longer works. - Lightness

1 answer 1

Try so, litter corrected.

 <Button.Visibility> <Binding ElementName="mainTab" Path="SelectedIndex" UpdateSourceTrigger="PropertyChanged"> <Binding.Converter> <local:BackButtonVisibleConverter/> </Binding.Converter> </Binding> </Button.Visibility> 

and in the converter use SelectedIndex

 if (1.Equals(value)) return Visibility.Visible; 

the fact is that PropertyChanged won't work, because mainTab itself mainTab not change, but if you specify the SelectedIndex property in the Path , then PropertyChanged for the Path work

  • Indeed, did not think about it. Your decision helped, thanks. - Lightness