我有一个包含一些控件的网格,我希望将控件的Isenabled属性绑定到网格的可见性属性。因此,如果网格是隐藏/折叠的,我希望禁用所有控件(或至少是文本框),并在网格可见的情况下启用它们。
<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>
答案 0 :(得分:1)
只需将以下属性添加到要启用禁用的控件中即可。
IsEnabled="{Binding ElementName=fItem, Path=Visibility, Converter={StaticResource visibilityToBoolConverter}}"
还要添加一个新的转换器文件,这里是转换器的代码
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();
}
}
在您的xaml文件中也添加以下行。
<Window.Resources>
<local:VisibilityToBoolConverter x:Key="visibilityToBoolConverter" ></local:VisibilityToBoolConverter>
</Window.Resources>
应该可以。如果您还有其他问题,请在评论中让我知道。
顺便说一下,您的代码的一个问题是,当gird隐藏/折叠时,您将无法看到控件,因为它们是您所在位置的网格的子代。