所以我试图在我的Viewmodel(Enum和Strings的混合)上将TextBlock绑定到多个值。当转换器返回文本为null时,我有一个假定的DataTrigger。但事实并非如此!起初,我认为我的风格没有抓住(因此改变了风格的背景以表明它的确如此)。无论如何这里是代码
XAML
<TextBlock x:Name="MyTextBlock" Grid.Column="2" Grid.ColumnSpan="3" VerticalAlignment="Center" DataContext="{StaticResource ViewModelLocator}"
Margin="{Binding RelativeSource={RelativeSource Self}, Path=(params:General.BoldPadding), Mode=OneWay}">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource GeneralMultiStringDisplayConverter}">
<Binding Path="RatesViewModel.Instrument.Currency" NotifyOnSourceUpdated="True" UpdateSourceTrigger="PropertyChanged"/>
<Binding Path="RatesViewModel.Instrument.Underlying" NotifyOnSourceUpdated="True" UpdateSourceTrigger="PropertyChanged"/>
<Binding Path="RatesViewModel.Instrument.ProductType" NotifyOnSourceUpdated="True" UpdateSourceTrigger="PropertyChanged"/>
</MultiBinding>
</TextBlock.Text>
<TextBlock.Resources>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource HeaderTextStyle}">
<Setter Property="Background" Value="Blue"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=MyTextBlock, Path=Text}" Value="{x:Null}"> <!--THIS SHOULD FIRE-->
<Setter Property="Text" Value="ThisShouldFireOnStart"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Resources>
</TextBlock>
转换器如下:
class GeneralMultiStringDisplayConverter:IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var AS = DependencyProperty.UnsetValue;
if (values[0] != AS )
{
int count = values.Count();
string result = string.Empty;
for (int i = 0; i < count - 1; ++i)
{
try
{
var A = Enum.GetName((values[i].GetType()), values[i]);
result = String.Format("{0}{1}.", result, A);
}
catch (Exception ex)
{
result = String.Format("{0}{1}.", result, values[i]);
}
}
result = String.Format("{0}{1}", result, values[count - 1]);
return result;
}
return null;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return null;
//TODO:
}
}
调试我已采取的步骤
`<Setter Property="Text" Value="ABC"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=MyTextBlock, Path=Text}" Value="ABC">
<Setter Property="Text" Value="ThisShouldFireOnStart"/>
</DataTrigger>
</Style.Triggers>
`
在样式中添加了默认的Text属性,并尝试更改基于该值的值。 (见上面的例子)
我希望得到一些帮助,让这个工作 谢谢!
答案 0 :(得分:0)
您无法使用Style中的DataTrigger覆盖这些本地绑定。 见Why doesn’t my WPF Trigger work?
如果您只需要显示字符串,如果您的绑定为null,请使用TargetNullValue="MyNullValueString"
也许您需要在转换器中使用if(value[0] != null)
条件来识别未设置的属性。由于我没有您的RatesViewModel的任何细节,我无法肯定地说。
<MultiBinding Converter="{StaticResource GeneralMultiStringDisplayConverter}" TargetNullValue="ThisIsNull">
<Binding Path="RatesViewModel.Instrument.Currency" NotifyOnSourceUpdated="True" UpdateSourceTrigger="PropertyChanged"/>
<Binding Path="RatesViewModel.Instrument.Underlying" NotifyOnSourceUpdated="True" UpdateSourceTrigger="PropertyChanged"/>
<Binding Path="RatesViewModel.Instrument.ProductType" NotifyOnSourceUpdated="True" UpdateSourceTrigger="PropertyChanged"/>
</MultiBinding>