我正在我的 wpf 应用程序中处理单选按钮和组合框。虽然我是一名 C++ 开发人员,但我最近转向了 C#。我的应用程序处理上述组件的动态生成。基本上我在我的应用程序中创建了 4 个动态单选按钮,单击每个按钮时,我应该将不同的项目添加到我的组合框中。这是代码:
XAML:
<ItemsControl ItemsSource="{Binding Children}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" >
<RadioButton Content="{Binding RadioBase}" Margin="0,10,0,0" IsChecked="{Binding BaseCheck}" GroupName="SlotGroup" Height="15" Width="80" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ComboBox Visibility="{Binding IsRegisterItemsVisible}" ItemsSource="{Binding RegComboList}" SelectedItem="{Binding SelectedRegComboList, Mode=TwoWay}" SelectedIndex="0" />
FPGARadioWidgetViewModel 类:
public ObservableCollection<FPGAViewModel> Children { get; set; }
public FPGARadioWidgetViewModel()
{
Children = new ObservableCollection<FPGAViewModel>();
Children.Add(new FPGAViewModel() { RadioBase = "Base 0x0", ID = 0 });
Children.Add(new FPGAViewModel() { RadioBase = "Base 0x40", ID = 1 });
Children.Add(new FPGAViewModel() { RadioBase = "Base 0x80", ID = 2 });
Children.Add(new FPGAViewModel() { RadioBase = "Base 0xc0", ID = 3 });
}
FPGAViewModel 类:
private bool sBaseCheck;
public bool BaseCheck
{
get { return this.sBaseCheck; }
set
{
this.sBaseCheck = value;
AddComboItems();
this.OnPropertyChanged("BaseCheck");
}
}
private ObservableCollection<string> _RegComboList;
public ObservableCollection<string> RegComboList
{
get { return _RegComboList; }
set
{
_RegComboList = value;
OnPropertyChanged("RegComboList");
}
}
private void AddComboItems()
{
int baseRegister = 0x40 * ID;
ObservableCollection<string> combo = new ObservableCollection<string>();
for (int i = 0; i < 0x40; i++)
{
int reg = (i * 8) + baseRegister;
combo[i] = "0x" + reg.ToString("X");
}
RegComboList = new ObservableCollection<String>(combo);
OnPropertyChanged("RegComboList");
}
private bool isRegisterItemsVisible = false;
public bool IsRegisterItemsVisible
{
get { return isRegisterItemsVisible; }
set
{
isRegisterItemsVisible = value;
OnPropertyChanged("IsRegisterItemsVisible");
OnPropertyChanged("RegComboList");
}
}
如果您注意到,在单击特定单选按钮时,它应该根据 ID 在组合框中添加具有不同值的项目。必须确保在单击任何单选按钮时,仅应添加该项目,并且应清除组合框的先前内容。我正在尝试使用上面的代码做同样的事情,但是当我调试时,组合框中似乎没有出现任何内容。
请帮忙 :)