0
votes

So I'm trying to Bind a TextBlock to multiple values on my Viewmodel (Mix of Enums and Strings). I have a DataTrigger that is supposed to fire when the text is null when returned by the Converter. But it doesn't! At first, I thought my Style didn't take hold (hence changed the Background on the Style to show it did). Anyway here is the code

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>

The Converter is as follows:

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:
    }
}

Debugging Steps that I have taken

    `<Setter Property="Text" Value="ABC"/>
         <Style.Triggers>
<DataTrigger Binding="{Binding ElementName=MyTextBlock, Path=Text}" Value="ABC"> 
<Setter Property="Text" Value="ThisShouldFireOnStart"/>
 </DataTrigger>
   </Style.Triggers>
                           `
  1. Added a converter to the Styles DataTrigger Binding. It always gets "" as a parameter and not null for some reason. Setting the trigger Value to "" does not work.
  2. Added a default Text property in the Style and tried changing the Value based off of that. (See example above)

    I would appreciate some help in getting this to work Thanks!

1

1 Answers

0
votes

You can not override these local bindings with a DataTrigger in a Style. See Why doesn’t my WPF Trigger work?

If you just need to show a string if your bindings are null use TargetNullValue="MyNullValueString"

Perhaps you need a if(value[0] != null) condition in your converter to identify unset properties. Since i do not have any detail of your RatesViewModel i can not say for sure.

<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>