簡體   English   中英

在運行時綁定 class 的屬性(在運行時創建 ViewModel)

[英]Bind properties of a class at runtime (Create ViewModel at runtime)

不知道如何提出我的問題,但我有多個類,每個 class 假設 Command51、Command49、Command00 包含多個屬性,我想要實現的是一種向用戶呈現具有多個文本框的視圖的方法,文本框的數量由 class 的屬性數量決定。 對於示例而言,Command51 將有 5 個屬性,Command49 將有 4 個屬性,而 Command00 將只有 1 個屬性:

假設我有一個文本框,用戶可以在其中輸入一個數字:51 或 49 或 00。

一旦在此文本框下鍵入數字,如果用戶輸入數字 51,則會出現 5 個文本框,如果用戶輸入數字 49,則會出現 4 個文本框,依此類推。

我想要的是將這些屬性動態綁定到文本框,我的主要重點是通過此文本框初始化 class 屬性。

Class 示例:

public class Command51 
{
   
    public Command51()
    {

    }

    public byte Prop1 { get; set; }
    public int Prop2 { get; set; }
    public float Prop3 { get; set; }
    public double Prop4 { get; set; }
    public string Prop5 { get; set; }

}

public class Command49 
{
   
    public Command49()
    {

    }

    public byte Prop1 { get; set; }
    public int Prop2 { get; set; }
    public byte Prop3 { get; set; }
    public int Prop4 { get; set; }
}

我現在需要使用反射但不知道如何..

   <ItemsControl
        VerticalAlignment="Top"
        ItemsSource="{Binding ListOfReflectedSetters}"
        ScrollViewer.VerticalScrollBarVisibility="Auto">
        <ItemsControl.ItemTemplate>
            <DataTemplate DataType="{x:Type /*DynamicDataType*/}">
                <Grid Margin="5">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <TextBlock
                        Grid.Column="0"
                        Margin="0,0,5,0"
                        Text="{Binding PropertyName}" />
                    <TextBox Grid.Column="1" Text="{Binding PropertyValue}" />
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

我將此代碼放在您擁有的一個TextBox的事件TextChanged上,這效率不高,因此請根據需要進行更改。 您可以通過這種方式獲取命名空間中的所有類

Type[] AllTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => 
String.Equals(t.Namespace, **YOURNAMESPACENAME**, StringComparison.Ordinal)).ToArray();

然后你遍歷類型並找到你想要的class

foreach (Type type in AllTypes)
{
    if (type.Name == TargetClassName)
        {
            //Do Stuff
        }
}

例如,在您的情況下//Do Stuff將制作多個文本框,因此首先我們獲取 Class 中的屬性數量:

//get number of properties
int PropertyCount = type.GetProperties().Length;

然后我們制作一個文本框列表並添加它們:

//make a list of textboxes for it
List<TextBox> TextBoxes = new List<TextBox>(PropertyCount);

for (int i = 0; i < PropertyCount; i++)
{
    PropertyInfo ThisProperty = type.GetProperties()[i];

    TextBox tb = new TextBox()
    {
        Text = ThisProperty.Name,
        Name = "TB" + i,
        Top = i * 32 + 44,
        Left = 12,
        Height = 20,
        Width = 200,
    };
    //you can add a textchange event here for any of textboxes
    tb.TextChanged += (s, ev) =>
    {
        //Do New Stuff
    };

    TextBoxes.Add(tb);

    Controls.Add(TextBoxes[i]);
}

我希望我沒有誤解,這回答了你的問題......這實際上是我在這里的第一篇文章。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM