views:

249

answers:

3

Can anyone tell me why the following doesn't work, but the one after it does? Notice the Value= syntax versus the explicit usage on the latter. I don't understand the difference.

<Style.Triggers>
    <DataTrigger Binding="{Binding ItemType}" Value="{x:Type log:FranchiseAiring}">
        <Setter Property="Template" Value="{StaticResource FranchiseRowStyle}" />
    </DataTrigger>
</Style.Triggers>

Above throws an exception, below works fine:

<Style.Triggers>
<DataTrigger Binding="{Binding ItemType}">
    <DataTrigger.Value>
        <x:Type Type="{x:Type log:FranchiseAiring}" />
    </DataTrigger.Value>
    <Setter Property="Template" Value="{StaticResource FranchiseRowStyle}" />
</DataTrigger>

Exception: Must specify both Binding and Value for DataTrigger. Error at object 'System.Windows.DataTrigger' in markup file ';component/ResourceDictionaries/LogStyles.xaml' Line 14 Position 15.

Stack Trace: at System.Windows.Markup.XamlParseException.ThrowException(String message, Exception innerException, Int32 lineNumber, Int32 linePosition, Uri baseUri,

+2  A: 

It is a bug that has been fixed. Check this: http://social.msdn.microsoft.com/Forums/en/wpf/thread/3fd23613-6f1d-4ae6-a279-b99d6fdbc374

viky
+2  A: 

It it probably caused by the bug Anurag linked to, but you should be aware that the two examples you used are not precisely identical from a XAML point of view.

If you convert this to element property syntax

Value="{x:Type whatever}"

what you get is:

<DataTrigger.Value>
  <x:Type TypeName="whatever">
</DataTrigger.Value>

What you wrote in your question actually corresponds to

Value="{x:Type Type={x:Type whatever}}"

Because of the semantics of TypeExtension, both ought to produce the same value in every situation I can think of. But for other purposes they may be different, and they may tickle different bugs in WPF.

Because of this, it is possible that Value="{x:Type Type={x:Type log:FranchiseAiring}}" might work for you. You may want to try it and find out.

Ray Burns
+1. This is because `Type` is marked as a `[ConstructorArgument]` property. But in this case, the behaviour should be identical.
Drew Noakes
Thanks, the Value="{x:Type Type= syntax worked perfectly.
thedesertfox
A: 

I tried the following and it does not seem to work.<Style x:Key="textboxStyle" TargetType="{x:Type TextBox}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=Property.PropertyType}" Value="{x:Type Type={x:Type sys:String}}"> <Setter Property="Visibility" Value="Visible" /> </DataTrigger> </Style.Triggers> </Style>

Boris Kleynbok