Reputation: 231
I have a grid which contains some controls, i wish to bind Isenabled property of controls to visibility property of grid. So, if the grid is hidden/collapsed i wish to disable all controls (or at least textboxes) and enable them if grid is visible.
<Grid x:Name="fItem" Visibility="hidden" HorizontalAlignment="Left" Height="43" Margin="10,73,0,0" VerticalAlignment="Top" Width="669">
<TextBox x:Name="fJm" HorizontalAlignment="Left" Height="23" Margin="35,9,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="54"/>
<Label x:Name="label_Copy" Content="J.m." HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
<Button x:Name="button" Content="Roba" HorizontalAlignment="Left" Margin="94,10,0,0" VerticalAlignment="Top" Width="79"/>
<TextBox x:Name="fNaziv" HorizontalAlignment="Left" Height="23" Margin="178,9,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="302"/>
<Label x:Name="label1" Content="Pg" HorizontalAlignment="Left" Margin="485,10,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="fPg" Height="23" Margin="503,9,0,0" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Left" Width="22"/>
<Label x:Name="label1_Copy" Content="Cijena:" HorizontalAlignment="Left" Margin="530,9,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="fCijena" Height="23" Margin="570,8,0,0" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Left" Width="78"/>
</Grid>
Upvotes: 0
Views: 1131
Reputation: 1209
Just add following property to your control which you want to enable disable.
IsEnabled="{Binding ElementName=fItem, Path=Visibility, Converter={StaticResource visibilityToBoolConverter}}"
And also add a new converter file here is code of converter
public class VisibilityToBoolConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var visibility = (Visibility) value;
if (visibility == Visibility.Collapsed || visibility == Visibility.Hidden)
return false;
return true;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
In your xaml file add following line as well.
<Window.Resources>
<local:VisibilityToBoolConverter x:Key="visibilityToBoolConverter" ></local:VisibilityToBoolConverter>
</Window.Resources>
It should work. If you have any other question let me know in comment.
By the way one problem with your code is when gird is hidden/collapsed you won't be able to see the controls are they are child of grid on which you are sitting visibility.
Upvotes: 2