2012-10-11 40 views
1

我是C++開發者和上週我開始在C#和WPF工作動態生成的UI組件。我有一個關於在我的代碼中使用結構的非常簡單的查詢。我的xaml類中有一個標籤和按鈕,應該是動態生成的。我用C++開發如下:使用結構數組在C#

typedef struct 
{ 
    String ChannelName; 
    bool available; 
} Voltage_Channel; 

Voltage_Channel *m_voltageChannels; 

Voltage_Channel redhookChannels[6] = { 
    {"", false}, 
    {"VDD_IO_AUD", true}, 
    {"VDD_CODEC_AUD", true}, 
    {"VDD_DAL_AUD", true}, 
    {"VDD_DPD_AUD", true}, 
    {"VDD_PLL_AUD", true},  
}; 

m_voltageChannels = redhookChannels; 

int cnt = 0; 
int MAX_ADC =6; 

while(cnt < MAX_ADC) 
{ 
    m_labelChannel[cnt] = new Label(m_voltageChannels[cnt].ChannelName); //Generates labels based on count 
    m_setButton[cnt] = new TextButton("Set"); //generates Buttons based on Count 
    m_setButton[cnt]->addButtonListener(this); // Event for the Button 

    if(m_voltageChannels[cnt].available) 
    { 
     addAndMakeVisible(m_labelChannel[cnt]); //Displays if channel is available 
     addAndMakeVisible(m_setButton[cnt]); 
    } 
} 

這將生成標籤和按鈕6次,如果通道可用,然後顯示它。

我在XAML中文件如下:

<Label Grid.Column="0" Content="{Binding VoltageLabel}" Name="VoltageLabel" /> 
<Button Grid.Column="1" Content="Set" Command="{Binding Path=SetButtonCommand}" Name="VoltageSetbtn" /> 

標籤和按鈕屬性:

string voltageLabel; 
public string VoltageLabel 
{ 
     get { return voltageLabel; } 
     set 
     { 
      voltageLabel = value; 
      OnPropertyChanged("VoltageLabel"); 
     } 
} 

    // Event for Set Button Click 
    private ICommand mSetCommand; 
    public ICommand SetButtonCommand 
    { 
     get 
     { 
      if (mSetCommand == null) 
       mSetCommand = new DelegateCommand(new Action(SetCommandExecuted), new Func<bool>(SetCommandCanExecute)); 

      return mSetCommand; 
     } 
     set 
     { 
      mSetCommand = value; 
     } 
    } 

    public bool SetCommandCanExecute() 
    { 
     return true; 
    } 

    public void SetCommandExecuted() 
    { 
     // Body of the method 
    } 

是否有可能使用結構這些控件,看如何動態生成我在我的C +應用程序中完成了它?

請幫

+0

除了:在Windows 8上開發時,您也可以使用C++的XAML。你爲什麼使用C#? – Beachwalker

+0

@Stegi:我沒有爲Windows 8開發。我在我的C++代碼中使用了JUCE,我必須在WPF中做同樣的事情:) – StonedJesus

回答

1

在WPF能夠直接映射控制要顯示針對對象,所以例如我現在有,

<DataTemplate DataType="{x:Type ViewModel:Clock_t}"> 
    <View:Clock_tView/> 
</DataTemplate> 

和這會自動將我的Clock_t類用ClockT_View封裝在UI中。

但是你會發現你需要一些助手來處理你的視圖,並且你不想污染你的模型,所以有一種模式說如果你願意的話在中間引入一些東西,一個ViewModel。這種模式被稱爲MVVM和Josh Smith's MVVM pattern article

解釋要回答你的問題,是你可以做這樣的事情

<ItemsControl ItemsSource="{Binding m_voltageChannels}"/> 

,並會自動嘗試顯示VoltageChannel,但除非你有一個DataTemplate你只是得到其ToString()(通常是班級名稱)的結果六次。相反,要麼在某個地方的資源中定義一個更高的DataTemplate,要麼只是嘗試

<ItemsControl ItemsSource="{Binding m_voltageChannels}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Vertical"> 
        <TextBlock Text="{Binding ChannelName}"/> 
        <CheckBox Content="Is available?" 
         IsChecked="{Binding available}"/> 
       </StackPanel> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
1

所有可用通過聲明XAML的方式控制可以在運行時代碼被添加了。所以你可以在while/for/foreach循環中將它們添加到你的視圖中。所有你需要的是父容器。

更優雅的方式是創建XAML中的模板,並綁定該模板包含數據的列表。

暗示自動生成的用戶界面: 也許是從M $爲數據使用內置PropertyGrid控件(如Visual Studio的屬性窗口中所示)。您可以附加數據元素的標準/自定義編輯器。也有幾個「自制的」物業網格可用。

下面是一個簡單的例子,一個鏈接如何使用模板:http://wblum.org/listbind/net3/index.html

這個問題也涉及到運行時間控制代:Add WPF control at runtime

+0

感謝您的回覆。是否有可能向我展示如何開始的示例代碼? :) – StonedJesus

1

在C#中控制簡單對象。最終,XAML中的每個功能都可以編碼。 XAML只是描述UI對象及其之間關係的一種更方便的方式。

每個組件都有,您可以使用添加子元素兒童屬性。 添加按鈕形式應該這樣工作:

Button myButton= new Button(); 
form.Children.Add(myButton);