簡體   English   中英

如何在WPF C#代碼隱藏文件中從預定義的值集中分配屬性?

[英]How in WPF C# code-behined file to assign a property from predefined set of values?

在我的WPF項目中,我有一個分配了一些屬性的自定義控件,它們是“字符串”和“布爾”類型的。 就像是:

public class CustControl : Control
{
    static CustControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustControl), new FrameworkPropertyMetadata(typeof(CustControl)));

    }

    public readonly static DependencyProperty CustNoProperty = DependencyProperty.Register("CustNo", typeof(string), typeof(CustControl), new PropertyMetadata(""));

    public string CustNo
    {
        get { return (string)GetValue(CustNoProperty); }
        set { SetValue(CustNoProperty, value); }
    }

    public readonly static DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(CustControl), new PropertyMetadata(false));

    public bool IsSelected
    {
        get { return (bool)GetValue(IsSelectedProperty); }
        set { SetValue(IsSelectedProperty, value); }
    }
....
}

現在,我必須在此處添加一個屬性,用於從一組預定義的值中選擇一個字符串值,例如“紅色”,“綠色”,“黃色”,“黑色”。

正確的方法是什么?

如果您的預定義字符串和示例中的一樣簡單,那么如何使用該屬性類型的枚舉呢? 通過對枚舉值調用ToString()可以輕松獲得字符串值。

暫無
暫無

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

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