我正在基於單選按鈕單擊動態生成標籤。那麼我已經成功做到了,但每次點擊一個按鈕時,它都會生成標籤,但不清除以前的狀態。下面是代碼:未能清除之前在單選按鈕上顯示的標籤單擊wpf
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個選定單選按鈕的標籤。這怎麼能實現?
感謝您的答覆。可以用示例代碼來詳細說明嗎?我認爲這真的會有所幫助:) –
請參閱編輯....... – GazTheDestroyer
@SteveWilson這是我第三次看到您要求提供示例代碼來執行此操作。我已經向你解釋了你需要在你的對象之間創建你需要的分層關係來實現這一目標的想法。請不要懶惰,不要再問別人做你的工作。 =) –