2011-03-09 5 views
1

요소의 값을 첫 번째 리소스로 바인딩하려고합니다. 그렇지 않으면 다른 리소스로 바인딩하려고합니다. 즉2 개의 DynamicResources에 대해 어떻게 바인딩 할 수 있습니까?

, 자원이 <s:String x:Key="first">Hello<s:String> <s:String x:Key="second">World<s:String>

처럼 보면

그러나 자원이있는 경우에만

<s:String x:Key="second">World<s:String>

헬로

를 개최 할 내 요소의 값 가치는 세계 일 것이다

많은 솔루션을 시도했지만 아무 것도 작동하지 않거나 충분히 우아합니다.

나는 컨버터가 최초의 null 이외의 값을 찾는 돌봐 어디

<MyElement> <MyElement.Value><MultiBinding Converter=...><DynamicResource Key=First/><DynamicResource Key=Second/> ...

를 작성할 수 바랍니다.

그러나, WPF는 해결책이 있습니까 DynamicResource 및 MultiBinding

을 혼합 할 수 없습니다?

답변

0

편집 1 : 귀하의 질문을 너무 빨리 읽었을 수 있습니다 ... 동적 리소스에 바인딩하고 있습니다 ... 클래스 속성이 아닙니다 ... 그래서 아래 해결책은 아마도 당신이 원하는 것이 아닙니다. 하지만 해결책을 찾는 데 도움이 될 경우를 대비하여 지금 맡겨 둘 것입니다.

편집 2 : Visual Studio 2010 SP1에서 다음 코드를 시도했으며 디자이너 (commentout/uncomment the resource 'first')에서와 같이 작동합니다. 그러나 내가 이상한 찾을 수있는 ... 성공적으로 구축 실패

<TextBlock> 
    <TextBlock.Resources> 
     <!--<s:String x:Key="first">Hello</s:String>--> 
     <s:String x:Key="second">World</s:String> 
     <l:NullItemConverter x:Key="NullItemConverter" /> 
    </TextBlock.Resources> 
    <TextBlock.Text> 
     <MultiBinding Converter="{StaticResource NullItemConverter}"> 
      <Binding Source="{DynamicResource first}" /> 
      <Binding Source="{DynamicResource second}"/> 
     </MultiBinding> 
    </TextBlock.Text> 
</TextBlock> 

public class NullItemConverter : IMultiValueConverter 
{ 
    object IMultiValueConverter.Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return values[0] ?? values[1]; 
    } 

    object[] IMultiValueConverter.ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

원래 대답 (즉, 귀하의 질문에 대답하지 않고 상황에 따라 도움이 될 수 있습니다) : 당신의 두 가지 속성에

는 가정이다 똑같은 클래스를 사용하면 올바른 값을 현명하게 출력하고 대신 바인딩 할 수있는 세 번째 속성을 만들 수 있습니까?

public class MyObject : INotifyPropertyChanged 
{ 
    private string property1; 
    public string Property1 
    { 
     get { return this.property1; } 
     set 
     { 
      if (this.property1 != value) 
      { 
       this.property1 = value; 
       NotifyPropertyChanged("Property1"); 
       NotifyPropertyChanged("PropertyForBinding"); 
      } 
     } 
    } 

    private string property2; 
    public string Property2 
    { 
     get { return this.property2; } 
     set 
     { 
      if (this.property2 != value) 
      { 
       this.property2 = value; 
       NotifyPropertyChanged("Property2"); 
       NotifyPropertyChanged("PropertyForBinding"); 
      } 
     } 
    } 

    public string PropertyForBinding 
    { 
     get 
     { 
      return this.Property1 ?? this.Property2; 
     } 
    } 

    public MyObject() { } 

    #region -- INotifyPropertyChanged Contract -- 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(String info) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
     } 
    } 

    #endregion INotifyPropertyChanged Contract 
} 
관련 문제