Редактировать 1: Возможно, я прочитал ваш вопрос слишком быстро ... вы привязываетесь к динамическим ресурсам ... не к свойствам класса ... поэтому приведенное ниже решение, вероятно, не то, что вы ищете. Но я оставлю это на время, если оно поможет вам найти решение.
Редактировать 2: я попробовал следующий код в Visual Studio 2010 с пакетом обновления 1 (SP1), и он работает так же, как и в конструкторе (закомментируйте / раскомментируйте ресурс '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
}