DataTrigger Error

2019-06-11 05:27发布

问题:

I am working with some multi-trriggers as per this posting. The binding and error are below.

I am using a custom markup extension to display the resource image so it could be suspect but I don't think so since I have used it in styles before.

The error message says I am applying the wrong property for the type and I don't see why yet.

Cheers,
Berryl

triggers

<Style x:Key="AvatarPathImageStyle" TargetType="{x:Type Image}">
    <Setter Property="Source" Value="{Binding AvatarPath}"/>
    <Setter Property="Height" Value="96"/>
    <Setter Property="Width" Value="96"/>
    <Setter Property="Stretch" Value="UniformToFill"/>
    <Style.Triggers>

        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding AvatarPath}" Value="{x:Null}" />
                <Condition Binding="{Binding Gender}" Value="{x:Static domain:Gender.Female}"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Source" Value="{resx:Resx ResxName=Smack.Parties.Presentation.Resources.PersonDetailView, Key=Img_Female}"/>
        </MultiDataTrigger>

        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding AvatarPath}" Value="{x:Null}" />
                <Condition Binding="{Binding Gender}" Value="{x:Static domain:Gender.Male}"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Source" Value="{resx:Resx ResxName=Smack.Parties.Presentation.Resources.PersonDetailView, Key=Img_Male}"/>
        </MultiDataTrigger>
        </MultiDataTrigger>

    </Style.Triggers>
</Style>

binding

<Image Grid.Column="1" Grid.Row="4" 
       HorizontalAlignment="Center" Margin="10, 0" Style="{StaticResource AvatarPathImageStyle}"
      />

error

System.Windows.Markup.XamlParseException occurred
...  InnerException: System.ArgumentException
       Message='System.Windows.Controls.Image' is not a valid value for the 'System.Windows.Controls.Image.Source' property on a Setter.
 ...

update

Accessing the image statically also gets an Invalid Type error:

<Setter Property="Source" Value="{x:Static imgResources:PersonDetailView.Img_Female}"/>

回答1:

Comment converted to answer:

The reason for the error is that images stored in a resx file are of type System.Windows.Drawing.Image, which are not directly compatible with WPF. You can write a converter, but unless you have a strong reason for using a resource dictionary I would just embed the images and reference them by URL relative to the XAML file