2012-11-01 45 views
0

我正在基於單選按鈕單擊動態生成標籤。那麼我已經成功做到了,但每次點擊一個按鈕時,它都會生成標籤,但不清除以前的狀態。下面是代碼:未能清除之前在單選按鈕上顯示的標籤單擊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個選定單選按鈕的標籤。這怎麼能實現?

回答

1

我不確定,但可能WPF會因爲PropertyChangedRegisterLabels而發生混淆,因爲當數組對象本身沒有改變時,只有內容具有。

通常,如果你有在WPF已改變內容的集合,您使用的ObservableCollection相反,它支持INofityCollectionChanged告訴WPF,當它包含的項目已經改變(如你的Children做)

要麼,每次在GenerateLabels中創建一個新陣列,然後在最後將其分配給您的RegisterLabels屬性。

ps。爲什麼您的方法是靜態的?如果它只是一個普通的實例方法,它會更清潔,那麼您就不必通過currentItemindex

EDIT:第二方法(替換陣列)的實施例

public string[] RegisterLabels { get; private set; } 

private void Generatelabels() 
{ 
    string[] labels = new string[8]; 
    int baseRegister = 0x40 * ID; 

    for (int i = 0; i < 8; i++) 
    { 
     int reg = (i * 8) + baseRegister; 
     labels[i] = "Reg 0x" + reg.ToString("X"); 
    } 

    RegisterLabels = labels; 
    OnPropertyChanged("RegisterLabels"); 
} 
+0

感謝您的答覆。可以用示例代碼來詳細說明嗎?我認爲這真的會有所幫助:) –

+0

請參閱編輯....... – GazTheDestroyer

+0

@SteveWilson這是我第三次看到您要求提供示例代碼來執行此操作。我已經向你解釋了你需要在你的對象之間創建你需要的分層關係來實現這一目標的想法。請不要懶惰,不要再問別人做你的工作。 =) –