문제

    public class FontType
    {
        ...
        public String Name { get { return _name; } }
        public String DisplayName { get { return _displayName; } }
        public Font UseFont { get { return _font; } }
    }


bindFontTypes.DataSource = availableFonts;
comboFontType.DataSource = bindFontTypes;
comboFontType.ValueMember = "Key";
comboFontType.DisplayMember = ...???;

여기, bindFontTypes 바인딩소스입니다. availableFonts Key가 문자열이고 Value가 FontType의 개체인 Hashtable입니다.을 위한 comboFontType.DisplayMember 개체의 .DisplayName 속성을 사용하고 싶습니다.어떻게 지정하나요?가능합니까?

도움이 되었습니까?

해결책

설정하면 작동할 수도 있습니다.

comboFontType.DisplayMember = "Value";  // FontType

그리고 과부하 ToString() ~을 위한 FontType.

ToString()의 대안으로 콤보 상자의 Format 이벤트를 처리할 수 있습니다.

하지만 데이터바인딩이 이런 방식으로 작동하는지조차 확신할 수 없습니다.

다른 팁

사용하여 DisplayMember = "Value.DisplayName" 나는 마지막으로 추가 된 것을 해시 테이블에 추가하고있다 ... 나는 그들 모두를 얻기 위해 노력하고있다 ....

이것은 내가 한 일입니다 ... 그러나 해시 테이블의 마지막 항목 만 묶을 것입니다 ....

BindingSource src = new BindingSource();
            src.DataSource = new Hashtable 
            { 
            {
                "blah", 
                new FontType 
                { 
                    Name = "newFont", 
                    DisplayName = "new Font" 
                } 
                },
                { 
                    "another", 
                    new FontType 
                    {
                        Name = "anotherFont",
                        DisplayName = "another Font" 
                    } 
                    } 
            };
            comboBox1.DataSource = src;
            comboBox1.ValueMember = "Key";
            comboBox1.DisplayMember = "Value.DisplayName";
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top