2012-10-31 16 views
1

我正在WPF應用程序中首先生成4個單選按鈕,一旦我完成了它,我需要在每個按鈕單擊上生成文本框和標籤。未能在WPF中單選按鈕上生成動態文本框

  • 生成4個單選按鈕具有不同Content
  • 在按下每個按鈕,生成8個標籤具有不同Content基於點擊按鈕併產生64名的文本框,其只在每一個標籤已經與相關的8個文本框這樣的方式讀它。即,8×8

我已經成功在一定程度上通過如下這樣做:

XAML:

以我XAML,我已經在2行分割網格。第一行將有4個單選按鈕。第二行將被分成2列,其中第一列將有8個動態標籤,第二列將有64個文本框。即每行8個。

<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> 

     <Button Content="Refresh Regs" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1" Margin="0" Width="100" Height="25" /> 
    </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> 

     <ItemsControl ItemsSource="{Binding Children}" Grid.Column="1"> 
      // Textbox here 
     </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"); 
     } 
    }   

    private string _RadioBase; 
    public string RadioBase 
    { 
     get; set; 
    } 

    private int _ID; 
    public int ID 
    { 
     get; set; 
    } 

因此,如果您發現上面的視圖模型類,指數給我單擊單選按鈕,我可以生成具有不同值的標籤基於計算。十六進制轉換完成。

這裏是要求:

  • 當我點擊一個單選按鈕,標籤越來越顯示Grid.Column="0",但我想文本框也得到相應的安置。如前所述,每個按鈕點擊將顯示64個盒子,每個標籤8個。文本框必須顯示在Grid.Column="1"中。

  • 當我點擊一個radibutton時,顯示標籤。當我點擊下一個按鈕時,標籤再次顯示。但之前顯示的標籤未被清除。我想在顯示新標籤之前清除它們。

  • 在啓動時,必須檢查第1個單選按鈕,並且必須顯示關聯的標籤+文本框。

示例代碼,我用C做++在那裏我能創造文本框:

for(i = 0; i < 0x40; i++) 
{ 
    m_registerGetValue[i] = new TextEditor(); 
    m_registerGetValue[i]->setReadOnly(true); 
    addAndMakeVisible(m_registerGetValue[i]); 
} 

下面是截圖:For further details 請幫助:)

+0

昨天你不是問這個問題嗎? – MyCodeSucks

+0

@Prayos:不,那是[http://stackoverflow.com/questions/13133800/program-crashes-when-dynamically-generated-radiobuttons-are-created-in-wpf] –

+0

我記得昨天看到這個確切的問題。所以,我查看了我的歷史,發現了[如何動態生成文本框,單擊動態生成的單選按鈕](http://stackoverflow.com/questions/13141612/how-to-generate-textboxes-dynamically-on-clicking-dynamically -generated-radiobut),這是一個已被刪除的問題。所以,雖然我無法用一張破損的清單證明任何事情,但我確定你昨天問過這個問題。 – MyCodeSucks

回答

1

我會採取不同的做法爲此:

  • 你收音機的虛擬機是可以的,但他們應該也是一個孩子包含網格中每個「行」的集合,其中包含一個「標題」屬性(您要放在標籤上的一個屬性)以及8個字符串值的列表,這些值將顯示在您的網格中文本框。喜歡的東西:

主視圖模型

  1. - >列表收音機VMS的
  2. - >選擇的無線電VM
    1. - >兒童 「行」
      1. 名單
      2. - >行標籤(字符串屬性)
      3. - > List of s特林值

那麼你就只需要2個itemscontrols:1繪製所有的「行」和每行,標籤,第二ItemsControl的(與水平的StackPanel在裏面ItemsPanelTemplate繪製文本框)

+0

好吧,這似乎是一個好主意。如果你能用代碼證明,將不勝感激:) –

相關問題