我正致力于基于radiobutton点击动态生成标签。好吧,我已经成功地做到了,但每次我点击一个按钮,它会生成标签,但不清除以前的状态。这是代码:
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");
}
}
在radiobutton上单击,它会显示8个标签。当我点击第二个radiobutton时,它会显示另一个8并且不会清除之前的8.基本上一次只能显示8个选定的radiobutton标签。怎么能实现呢?
答案 0 :(得分:1)
我不确定,但可能WPF因为你为PropertyChanged
解雇RegisterLabels
而感到困惑,当数组对象本身没有改变时,只有内容有。
通常,如果您的集合在WPF中有更改内容,则使用ObservableCollection
来支持INofityCollectionChanged
告诉WPF它包含的项目何时发生更改(就像您对{{1 }})
要么是这样,要么每次都在Children
创建一个新数组,然后在最后将它分配给GenerateLabels
属性。
PS。为什么您的RegisterLabels
方法是静态的?如果它只是一个普通的实例方法会更加清晰,那么你就不必传入GenerateLabels()
和currentItem
。
编辑:第二种方法的示例(替换数组)
index