最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to change Image source using a triggerbehavior? - Stack Overflow

programmeradmin0浏览0评论

I have the following XAML markup, using WinUi 1.6.5, .NET 9 and C# 13.

This my DataTemplate for usage in an ItemView:

<DataTemplate x:DataType="MyModel">
    ...
    <Image x:Name="Image1" Grid.Row="0" Grid.Column="1" Height="16" />
    <Image x:Name="Image2" Grid.Row="1" Grid.Column="1" Height="16" />
</DataTemplate>

In my model class, I have the following properties:

public string Type => ""; // Can be either Case1 or Case2
public string TypeImageUri => "/Assets/picture.png";

Now I would like to assign that TypeImageUri to one of that Image controls depending on the Type property, like this:

<Image x:Name="Image1" Grid.Row="0" Grid.Column="1" Height="16">
    <interactivity:Interaction.Behaviors>
        <interactivity:DataTriggerBehavior Binding="{Binding Type}" ComparisonCondition="Equal" Value="Case1">
            <interactivity:ChangePropertyAction TargetObject="{Binding ElementName=Image1}" PropertyName="Source" Value="{x:Bind TypeImageUri}" />
        </interactivity:DataTriggerBehavior>
    </interactivity:Interaction.Behaviors>
</Image>

Unfortunately, it's not working, it seems that's not the meaning of the matter of Behaviors. Is there some way to make this approach work? I don't want to have to do this in code.

Best regards

I have the following XAML markup, using WinUi 1.6.5, .NET 9 and C# 13.

This my DataTemplate for usage in an ItemView:

<DataTemplate x:DataType="MyModel">
    ...
    <Image x:Name="Image1" Grid.Row="0" Grid.Column="1" Height="16" />
    <Image x:Name="Image2" Grid.Row="1" Grid.Column="1" Height="16" />
</DataTemplate>

In my model class, I have the following properties:

public string Type => ""; // Can be either Case1 or Case2
public string TypeImageUri => "/Assets/picture.png";

Now I would like to assign that TypeImageUri to one of that Image controls depending on the Type property, like this:

<Image x:Name="Image1" Grid.Row="0" Grid.Column="1" Height="16">
    <interactivity:Interaction.Behaviors>
        <interactivity:DataTriggerBehavior Binding="{Binding Type}" ComparisonCondition="Equal" Value="Case1">
            <interactivity:ChangePropertyAction TargetObject="{Binding ElementName=Image1}" PropertyName="Source" Value="{x:Bind TypeImageUri}" />
        </interactivity:DataTriggerBehavior>
    </interactivity:Interaction.Behaviors>
</Image>

Unfortunately, it's not working, it seems that's not the meaning of the matter of Behaviors. Is there some way to make this approach work? I don't want to have to do this in code.

Best regards

Share Improve this question edited Feb 17 at 8:12 Andrew KeepCoding 13.5k2 gold badges19 silver badges35 bronze badges asked Feb 16 at 17:22 xerxosxerxos 555 bronze badges 1
  • You can use value converter (IValueConverter). Bind to the Type and let converter to return the image you need depending of the property value. – Serg Commented Feb 16 at 20:32
Add a comment  | 

1 Answer 1

Reset to default 0

I agree with the suggestion in the comments. This seems to be easier with a value converter. Or with a function maybe. (I have a video for this.)

I tried to reproduce your issue and created a test app based on your code but it worked. My first guess is that the image files are not set as Content.

发布评论

评论列表(0)

  1. 暂无评论