我是一名 C++ 开发人员,最近转向 C#。我正在开发处理动态生成组框的 WPF 应用程序。相应组框中的 UI 组件(如 RadioButton、togglebutton 等)必须依次动态生成。
目前我有 2 个 xaml 文件CodecView.xaml
和两个 xaml 文件CodecWidgetView.xaml
的单独视图模型类。好吧,这个充满活力的一代很有趣,但对我来说肯定是一个棘手的情况。让我向您展示我在其中动态生成一组组框的代码。
CodecView.xaml:
<UserControl.Resources>
<DataTemplate x:Key="CWDataTemplate">
<StackPanel>
<TextBlock Text="{Binding Description}" Margin="5,5,0,0"/>
<local:CodecWidgetView Margin="5,10,5,5"/>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid Grid.Row="0" Style="{DynamicResource styleBackground}">
<Grid Name="NumberofCodecs" >
<ItemsControl ItemTemplate="{StaticResource CWDataTemplate}" ItemsSource="{Binding CodecWidgets}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</Grid>
CodecWidgetView.xaml:
<Grid>
<GroupBox Height="Auto" HorizontalAlignment="Stretch" Margin="5,5,5,5" Name="groupBox1" VerticalAlignment="Stretch" Width="Auto">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ToggleButton Name="MasterBox" Content="Master" Command="{Binding MasterCommand}" IsChecked="{Binding MasterCheck}" Grid.Column="1" Height="25" Width="50" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0" />
<ComboBox Grid.Column="2" ItemsSource="{Binding ModesList}" SelectedItem="{Binding SelectedModesList, Mode=OneWayToSource}" SelectedIndex="2" Height="23" HorizontalAlignment="Center" Margin="0,0,0,0" Name="comboBox2" VerticalAlignment="Center" Width="80" />
<ComboBox Grid.Column="0" ItemsSource="{Binding FrequencyList}" SelectedItem="{Binding SelectedFrequencyList, Mode=OneWayToSource}" SelectedIndex="0" Height="23" HorizontalAlignment="Center" Margin="0,0,0,0" Name="comboBox1" VerticalAlignment="Center" Width="80" />
</Grid>
<Grid Grid.Row="1">
//Here I want to dynamically generate 4 radio buttons
</Grid>
<Grid Grid.Row="2">
<Label Name="BitDelay" Content="Bit Delay" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,205,0" Height="25" Width="55" />
<Slider Height="23" HorizontalAlignment="Center" Value="{Binding BitDelayValue, Mode=TwoWay}" Minimum="0.0" Maximum="255.0" TickFrequency="1.0" Margin="95,0,0,0" Name="bitdelayslider" VerticalAlignment="Center" Width="160" />
<TextBox Name="BitDelayValue" IsReadOnly="True" Text="{Binding ElementName=bitdelayslider,Path=Value, StringFormat=0.0}" Width="40" Height="20" Margin="0,0,110,0" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
<Grid Grid.Row="3">
<Label Name="DBGain" Content="DB Gain" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,205,0" Height="25" Width="55" />
<TextBox Name="DBGainValue" IsReadOnly="True" Text="{Binding ElementName=dbgainslider,Path=Value, StringFormat=0.0}" Width="40" Height="20" Margin="0,0,110,0" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Slider Height="23" HorizontalAlignment="Center" Value="{Binding DBGainValue, Mode=TwoWay}" Minimum="0.0" Maximum="59.5" TickFrequency="0.5" Margin="95,0,0,0" Name="dbgainslider" VerticalAlignment="Center" Width="160" />
</Grid>
</Grid>
</GroupBox>
</Grid>
CodecViewModel.cs:
public ObservableCollection<CodecWidgetViewModel> CodecWidgets { get; set; }
public CodecViewModel()
{
CodecWidgets = new ObservableCollection<CodecWidgetViewModel>();
CodecWidgets.Add(new CodecWidgetViewModel { Description = "Location 8 - Dig Mic A" });
CodecWidgets.Add(new CodecWidgetViewModel { Description = "Location 9 - Dig Mic B" });
CodecWidgets.Add(new CodecWidgetViewModel { Description = "Location 10 - PCM A 3P3V" });
CodecWidgets.Add(new CodecWidgetViewModel { Description = "Location 11 - PCM B 3P3V" });
}
CodecWidgetViewModel.cs:
private string _description;
public string Description
{
get
{
return _description;
}
set
{
_description = value;
OnPropertyChanged("Description");
}
}
这在启动时给了我 4 个组框。现在是要求:
- 看一下
CodecWidgetView.xaml
,你会发现我添加了组合框、滑块等,但Grid.Row=2
我想动态生成 4 个切换按钮,内容为:16 Bit, 20 Bit, 24 Bit and 32 Bit
。 - 一旦发生这种情况,这个按钮上必须只有一个单击事件,它设置切换状态并执行一些语句。
这就是我在 C++ 中所做的:
//In Constructor
for(int jj = 0; jj < 4; jj++)
{
m_codecBitLengthButton[jj] = new ToggleButton(bitLengthes[jj]); //Here bitlength consists of 16Bit, 20Bit, 24Bit and 32Bit
m_codecBitLengthButton[jj]->setRadioGroupId(55); // make a custom group ID
addAndMakeVisible(m_codecBitLengthButton[jj]);
m_codecBitLengthButton[jj]->addButtonListener(this);
m_codecBitLengthButton[jj]->setToggleState(false, false);
}
// check which button should be pressed
cmd = (0x9400 | (m_slot & 0xFF));
m_msp430->ReadInternalCommand(cmd, 1, readBuf); //This returns some value in readBuf
m_bitLength = readBuf[0];
m_codecBitLengthButton[readBuf[0]]->setToggleState(true, false); //Togglestate set to true on startup based on readBuf[0]
//Gets called when any of the toggle button is clicked
while(jj < 4)
{
if(button == m_codecBitLengthButton[jj])
{
// add code for bit length support
cmd = ((CODEC_LENGTH_CMD & 0x7F00) | (m_slot & 0xFF));
sendBuf[numBytes++] = jj; // this should represent the proper number of bits
m_bitLength = jj;
break;
}
jj++;
}
我如何在 C# 中实现相同的目标?:)