我正在基于单选按钮点击动态生成标签。好吧,我已经成功了,但是每次单击按钮时,它都会生成标签,但不会清除先前的状态。这是代码:
XAML:
<Grid Grid.Row="0">
<ItemsControl ItemsSource="{Binding Children}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" >
<RadioButton Content="{Binding RadioBase}" Margin="0,10,0,0" IsChecked="{Binding BaseCheck}" GroupName="SlotGroup" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
<Grid Grid.Row="1">
<ItemsControl ItemsSource="{Binding Children}" Grid.Column="0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<ItemsControl Visibility="{Binding IsRegisterItemsVisible, Converter={StaticResource BoolToVisibilityConv}}" ItemsSource="{Binding RegisterLabels}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Margin="50,20,0,0">
<TextBlock Text="{Binding}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
FPGARadioWidgetViewModel类:这里设置这个类的DataContext
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;
Generatelabels(this, ID);
this.OnPropertyChanged("BaseCheck");
}
}
private static void Generatelabels(FPGAViewModel currentItem, int index)
{
int m_baseRegister = 0;
if (index == 0)
{
for (int i = 0; i < 0x40 / 8; i++)
{
int reg = (i * 8) + m_baseRegister;
currentItem.RegisterLabels[i] = "Reg 0x" + reg.ToString("X");
currentItem.IsRegisterItemsVisible = true;
}
}
else if (index == 1)
{
m_baseRegister = 0x40 * index;
for (int i = 0; i < 0x40 / 8; i++)
{
int reg = (i * 8) + m_baseRegister;
currentItem.RegisterLabels[i] = "Reg 0x" + reg.ToString("X");
currentItem.IsRegisterItemsVisible = true;
}
}
// Similarly for Index 2 and Index = 3
}
private string[] registerLabels = new string[8];
public string[] RegisterLabels { get { return registerLabels; } }
private bool isRegisterItemsVisible = false;
public bool IsRegisterItemsVisible
{
get { return isRegisterItemsVisible; }
set
{
isRegisterItemsVisible = value;
OnPropertyChanged("IsRegisterItemsVisible");
OnPropertyChanged("RegisterLabels");
}
}
单击单选按钮时,它会显示 8 个标签。当我单击第二个单选按钮时,它会显示另外 8 个并且不会清除前 8 个。基本上一次只必须显示所选单选按钮的 8 个标签。怎样才能做到这一点?