我有几个实例,用户可能需要根据组合框选择输入其他数据。必要文本框的数量根据选择而变化(最小1-最大3)。为了保持这些项目的有序性,我将它们放在GroupBox中。我需要的是达到这个效果的建议。我应该以编程方式将行添加到组框中的网格吗?我应该创建所有文本框和标签,并更改选择更改事件的可见性属性吗?我不太喜欢第二个想法,因为如果只需要一个文本字段,则组框将被不适当地缩放。我意识到我的问题的一般背景,作为WPF的新手,我只需要一些关于实现这一目标的最佳实践建议。
更新 我试图在选择更改时折叠该行。这摆脱了内容,但仍然会使我的组框大小相同。
<GroupBox Name="gbCondensor" Height="Auto" Header="Condensor"
FontSize="16" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2"
Margin="20,0,20,92" Grid.RowSpan="2">
<Grid Name="grdCondensor" Margin="0,10,0,0" Height="auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label x:Name="lblCondensorTemp" Grid.Column="0" Grid.Row="0"
Content="Condensor Temperature" HorizontalAlignment="Center"
VerticalAlignment="Center" Height="32" Width="255"/>
<ComboBox Name="cmbCondensorTemp" FontSize= "16" Width="200"
Grid.Column="1" Grid.Row="0" VerticalAlignment="Center"
HorizontalAlignment="Left" Margin="10,0" Height="30">
<ComboBoxItem IsSelected="True">Constant</ComboBoxItem>
<ComboBoxItem>Linear</ComboBoxItem>
<ComboBoxItem>Switching</ComboBoxItem>
</ComboBox>
<Label Name="lblCondensorTempText" Content="Temperature"
VerticalAlignment="Center" HorizontalAlignment="Right" Grid.Column="0"
Grid.Row="2" ></Label>
<TextBox Name="txtCondensorTemp" Grid.Column="1" Grid.Row="2"
Background="Gray" VerticalAlignment="Center" HorizontalAlignment="Center"
Width="73"></TextBox>
<Label Name="lblEndCondTemp" Content="Temperature"
VerticalAlignment="Center" HorizontalAlignment="Right" Grid.Column="0"
Grid.Row="3" ></Label>
<TextBox Name="txtEndCondTemp" Grid.Column="1" Grid.Row="3"
Background="Gray" VerticalAlignment="Center" HorizontalAlignment="Center"
Width="73"></TextBox>
<Label Name="lblSwitchCondTemp" Content="Temperature"
VerticalAlignment="Center" HorizontalAlignment="Right" Grid.Column="0"
Grid.Row="4" ></Label>
<TextBox Name="txtSwitchCondTemp" Grid.Column="1" Grid.Row="4"
Background="Gray" VerticalAlignment="Center" HorizontalAlignment="Center"
Width="73"></TextBox>
</GroupBox>
用这个来折叠代码中的Rows:
void CheckCondensorComboBox(object sender, SelectionChangedEventArgs e)
{
if (cmbCondensorTemp.SelectedIndex == 0)
{
lblEndCondTemp.Visibility = Visibility.Collapsed;
txtEndCondTemp.Visibility = Visibility.Collapsed;
grdCondensor.RowDefinitions[3].Height = new GridLength(0);
lblSwitchCondTemp.Visibility = Visibility.Collapsed;
txtSwitchCondTemp.Visibility = Visibility.Collapsed;
grdCondensor.RowDefinitions[4].Height = new GridLength(0);
}
else if (cmbCondensorTemp.SelectedIndex == 1)
{
lblEndCondTemp.Visibility = Visibility.Visible;
txtEndCondTemp.Visibility = Visibility.Visible;
grdCondensor.RowDefinitions[3].Height = new GridLength(30);
lblSwitchCondTemp.Visibility = Visibility.Collapsed;
txtSwitchCondTemp.Visibility = Visibility.Collapsed;
grdCondensor.RowDefinitions[4].Height = new GridLength(0);
}
else if (cmbCondensorTemp.SelectedIndex == 2)
{
lblEndCondTemp.Visibility = Visibility.Visible;
txtEndCondTemp.Visibility = Visibility.Visible;
grdCondensor.RowDefinitions[3].Height = new GridLength(30);
lblSwitchCondTemp.Visibility = Visibility.Visible;
txtSwitchCondTemp.Visibility = Visibility.Visible;
grdCondensor.RowDefinitions[4].Height = new GridLength(30);
}
}
我在这里想要的是一个组合框,它根据每个用户选择所需的控件来改变它的高度。
答案 0 :(得分:0)
你的两个想法都是有效的。在没有看到代码的情况下,问题有点难以回答,但一般来说我喜欢首先考虑可维护性 - 代码通常读的次数比编写的多很多倍。
我怀疑简单地折叠不需要的额外字段可能比创建额外字段并在运行时绑定它们更直接和可维护。
尝试一种方式,看看它的外观 - 不要花太长时间来衡量它。