I have an arrow image that rotates when an expander is expanded. I'm trying to convert the XAML code to C#, I can see the arrow and the app theme binding works in the c# code except that it is not rotating.
xaml:
<Image
HeightRequest="25"
HorizontalOptions="End"
Source="{AppThemeBinding Light=down_arrow.png,
Dark=down_arrow_dark.png}"
VerticalOptions="Start">
<Image.Triggers>
<DataTrigger
Binding="{Binding Source={RelativeSource AncestorType={x:Type mct:Expander}}, Path=IsExpanded}"
TargetType="Image"
Value="True">
<Setter Property="Rotation" Value="180" />
</DataTrigger>
</Image.Triggers>
</Image>
Current C# code:
var arrowImage = new Image()
{
Triggers =
{
new DataTrigger(typeof(Image))
{
Value = true,
Binding = new Binding(nameof(expander.IsExpanded)),
Setters =
{
new Setter()
{
Property = Image.RotationProperty,
Value = 180
}
}
}
},
HeightRequest = 25,
HorizontalOptions = LayoutOptions.End,
};
arrowImage.SetAppTheme<FileImageSource>(Image.SourceProperty, "down_arrow.png", "down_arrow_dark.png");
CodePudding user response:
I had to set the Source and have a trigger for Value = false
for it to behave the same as XAML.
var arrowImage = new Image()
{
Triggers =
{
new DataTrigger(typeof(Image))
{
Value = true,
Binding = new Binding(nameof(expander.IsExpanded), BindingMode.Default, null, null, null, expander),
Setters =
{
new Setter()
{
Property = Image.RotationProperty,
Value = 180
}
}
},
new DataTrigger(typeof(Image))
{
Value = false,
Binding = new Binding(nameof(expander.IsExpanded), BindingMode.Default, null, null, null, expander),
Setters =
{
new Setter()
{
Property = Image.RotationProperty,
Value = 0
}
}
},
},
HeightRequest = 25,
HorizontalOptions = LayoutOptions.End,
};