我有一个网格,我试图隐藏一个包含使用c#的文本框作为我的代码后面的一行。 我的最终目标的目的是找到一种方法来设置文本在行隐藏隐藏的文本框中。如果文本框的大小小于字体大小,我可能会遇到wpf不允许在文本框中设置文本的问题。这是我到目前为止有:XAML C#隐藏网格行
XAML:
Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<Button x:Name="Button1"
Grid.Row="2"
Grid.Column="1"
Width="100"
Height="50"
Click="OnClick"
Content="Hide Middle Row"/>
<Grid x:Name="AddressBar" Grid.Row="1" Grid.Column="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="Block1"
FontSize="16"
Grid.ColumnSpan="3"
HorizontalAlignment="Center"
TextAlignment="Center"/>
</Grid>
</Grid>
C#:
namespace rowCollapseTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void OnClick(object sender, RoutedEventArgs e)
{
AddressBar.RowDefinitions(1).Height = new GridLength(0);
AddressBar.Visibility = Visibility.Collapsed;
Block1.Text = "This is a test";
}
}
}
从我已阅读,这应该工作。但是,我收到有关“RowDefinitions(1)”的错误。该错误显示为:“不可调用的成员'Grid.RowDefinitions'不能像方法一样使用。”有任何想法吗?
提前致谢!
噢,呃,我怎么没有抓到!不幸的是,我使用方括号出现了界限错误。我应该可以这样写: AddressBar.Height = new GridLength(0); 但在这种情况下,我收到了错误:无法隐式地将类型System.Window.GridLength转换为'double'。 – user5890660
@ user5890660哦,对,AddressBar根本没有RowDefinitions。另一个网格有行定义,AddressBar只有列定义。 –
@ user5890660高度不是GridLength值,只是双倍值。将简单的旧零分配给它是好的 - 或者更好的是,将Visibility.Collapsed分配给其可见性属性。 –