본문 바로가기

Silverlight

SL4 문자열 인덱서로 바인딩 + 프로퍼티 인덱서


실버라이트4 Beta 에서 문자열 인덱서를 대상으로 바인딩이 가능 하게 되었는데요. 여기다가 프로퍼티 인덱서를 이용하면 이렇게도 바인딩이 가능하네요

우선 SL4에서 제공 되는 문자열 인덱서를 알아보면요
<TextBlock Text="{Binding Path=CustomProperties[NIckname]}"/>
위에 TextBlock의 DataContext 인 Employee 에는 CustomProperties 라는 사전형식의 프로퍼티가 있을때 키값을 이용해서 값을 바인딩이 가능한데요.


여기서 쵸큼만 더 나아가서 Employee에다가 다음과 같이 인덱서를 추가 하고요

public string this[string key]

        {

            get

            {

                if (_customProperties == null)

                {

                    _customProperties = new Dictionary<string, object>();

                    return string.Empty;

                }

                if (_customProperties.ContainsKey(key) == true)

                    return _customProperties[key].ToString();

                else

                    return string.Empty;

            }

            set

            {

                if (_customProperties == null)

                {

                    _customProperties = new Dictionary<string, object>();

                }

                _customProperties.Add(key, value);

            }

        }

 



그후에는 다음과 같이 바인딩이 가능하네요.

<TextBlock Text="{Binding [Nickname]}"/>



 


머 그렇다고요 -.,-;;