我是一名 C++ 开发人员,最近转向 C#。我正在开发一个 WPF 应用程序,我需要在其中动态生成按钮、文本框等 Ui 组件。这就是我到目前为止所做的。
XAML 类:
<Grid Visibility="{Binding IsAvailable, Converter={StaticResource booltovisibility}}">
<Grid.Resources>
<convert:BooleanToVisibilityConverter x:Key="booltovisibility"/>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="170" />
<ColumnDefinition />
<ColumnDefinition Width="130" />
<ColumnDefinition Width="115" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="{Binding ChannelName}" Height="25" Width="120" Name="VoltageLabel" Margin="20,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" />
<TextBox Grid.Column="1" Text="{Binding VoltageText}" Height="25" Width="65" Name="VoltageBox" Margin="0,0,80,0" VerticalAlignment="Center" HorizontalAlignment="Center" />
<Button Grid.Column="1" Content="Set" CommandParameter="{Binding VoltageText}" Command="{Binding VoltageCommand}" Height="25" Width="65" Name="VoltageSetbtn" Margin="80,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center" />
<Label Grid.Column="2" Content="{Binding CurrentText}" Height="25" Width="40" Name="CurrentLabel" Margin="0,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center" />
<ToggleButton Grid.Column="3" Content="On" Height="25" Width="30" Name="VoltageToggleBtn" Margin="0,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Grid>
<Button Content="Bavaria" Name="BavariaBtn" Click="BavariaBtn_Click" />
视图模型类:
public List<VoltageBoardChannel> channelList = null;
public List<VoltageBoardChannel> bavaria2Channels = new List<VoltageBoardChannel>
{
new VoltageBoardChannel { ChannelName = "VDD__MAIN", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
new VoltageBoardChannel { ChannelName = "VDD__IO__AUD", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
new VoltageBoardChannel { ChannelName = "VDD__CODEC__AUD", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand},
new VoltageBoardChannel { ChannelName = "VDD__DAL__AUD", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
new VoltageBoardChannel { ChannelName = "VDD__DPD__AUD", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
new VoltageBoardChannel { ChannelName = "VDD__PLL__AUD", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
new VoltageBoardChannel { ChannelName = "", IsAvailable = false, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
new VoltageBoardChannel { ChannelName = "", IsAvailable = false, VoltageText = String.Empty, VoltageCommand = m_voltageCommand }
};
private ICommand m_voltageCommand;
public List<VoltageBoardChannel> bavaria1Channels = new List<VoltageBoardChannel>
{
new VoltageBoardChannel { ChannelName = "", IsAvailable = false, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
new VoltageBoardChannel { ChannelName = "", IsAvailable = false, VoltageText = String.Empty, VoltageCommand = m_voltageCommand }
new VoltageBoardChannel { ChannelName = "VDD__MAIN", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
new VoltageBoardChannel { ChannelName = "VDD__IO", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
new VoltageBoardChannel { ChannelName = "VDD__CODEC", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
new VoltageBoardChannel { ChannelName = "VDD__LDO", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand },
new VoltageBoardChannel { ChannelName = "VDD__AMP", IsAvailable = true, VoltageText = String.Empty, VoltageCommand = m_voltageCommand }
};
public VoltageViewModel()
{
channelList = new List<VoltageBoardChannel>(0);
channelList = bavaria1Channels;
m_voltageCommand = new DelegateVoltageCommand(x => SetCommandExecute(x));
}
public List<VoltageBoardChannel> VoltageChannelList
{
get
{
return channelList;
}
set
{
channelList = value;
OnPropertyChanged("ChannelList");
}
}
public void SetCommandExecute(object voltageText)
{
Debug.WriteLine(voltageText);
}
型号类:
private string mChannelName;
public string ChannelName
{
get; set;
}
private bool mIsAvailable;
public bool IsAvailable
{
get; set;
}
string voltageText = string.Empty;
public string VoltageText
{
get; set;
}
string currentText = "0 V";
public string CurrentText
{
get; set;
}
public ICommand VoltageCommand { get; set; }
XAml.cs:
VoltageViewModel mVoltageViewModel = new VoltageViewModel();
public VoltageView()
{
InitializeComponent();
this.DataContext = mVoltageViewModel;
OnChildAdd();
}
public void OnChildAdd() //Constructor
{
VoltageViewModel mVoltageViewModel = new VoltageViewModel();
foreach (VoltageBoardChannel mVoltageChannelViewModel in mVoltageViewModel.VoltageChannelList)
{
VoltageChannelView mVoltageChannelView = new VoltageChannelView();
mVoltageChannelView.Margin = new Thickness(2);
mVoltageChannelView.ChannelInfo = mVoltageChannelViewModel;
// Some Code
}
}
这里它显示了 Bavaria 1 的所有频道,甚至是 one's where available = false
。因此,当它为 false 时,它会显示文本框、按钮、标签和切换按钮。频道名称是“”。我想实现以下目标:
我这里有 2 个频道,bavaria 1 和 bavaria 2。在启动时 bavaria1 已经显示。在这里,我想检查可用频道并仅将那些添加到我的启动视图中,即available = true
应该显示,而当available = false
不显示相应元素时。目前甚至available = false
显示按钮、文本框、切换按钮,除了标签(频道名称将是“”)。我怎样才能做到这一点?