【问题标题】:Dynamically generate UI Components using Array of Structures in C#在 C# 中使用结构数组动态生成 UI 组件
【发布时间】:2012-10-02 01:06:38
【问题描述】:

我是 C++ 开发人员,从上周开始我开始研究 C# 和 WPF。我有一个关于在我的代码中使用结构的非常简单的查询。我的 xaml 类中有一个标签和按钮,应该动态生成。我在 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+ 应用程序中是如何做到的?

请帮忙

【问题讨论】:

  • 另外:在为 Windows 8 开发时,您也可以使用 C++ 中的 XAML。你为什么使用 C#?
  • @Stegi:我不是为 Windows 8 开发的。我在我的 c++ 代码中使用了 JUCE,我必须在 WPF 中做同样的事情 :)

标签: c# c++ .net wpf dynamic


【解决方案1】:

在 WPF 中,可以直接映射控件以针对对象显示,例如我目前有,

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

这会自动在 UI 中用 ClockT_View 包装我的 Clock_t 类。

但是,您往往会发现您的 View 需要一些帮助程序,并且您不想污染您的模型,因此有一种模式表示要在中间引入一些东西,如果您愿意,可以使用 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>

【讨论】:

    【解决方案2】:

    在 C# 中,控件是简单的对象。最终,可以对 XAML 中存在的每个功能进行编码。 XAML 只是一种更方便的方式来描述 UI 对象以及它们之间的关系。

    每个组件都有一个 children 属性,您可以使用它来添加子元素。 向表单添加按钮应该是这样的:

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

    【讨论】:

      【解决方案3】:

      通过声明性 XAML 方式可用的所有控件也可以在运行时通过代码添加。因此,您可以在 while/for/foreach 循环中将它们添加到您的视图中。您只需要父容器。

      更优雅的方法是在 xaml 中创建一个模板并将该模板绑定到包含数据的列表。

      自动生成 UI 的提示: 也许可以选择使用 M$ 中的内置 PropertyGrid 控件来处理您的数据(如 Visual Studio 的属性窗口中所示)。您可以为您的数据元素附加标准/自定义编辑器。也有几个“自制”属性网格可用。

      这是一个如何使用模板的简单示例的链接:http://wblum.org/listbind/net3/index.html

      这个问题也和运行时控件生成有关:Add WPF control at runtime

      【讨论】:

      • 感谢您的回复。您是否可以向我展示如何开始的示例代码? :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-11
      • 1970-01-01
      • 2012-05-17
      相关资源
      最近更新 更多