Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
xmlns:vm="clr-namespace:CommunityToolkit.Maui.Sample.ViewModels.Behaviors"
Title="IconTintColorBehaviorPage"
x:DataType="vm:IconTintColorBehaviorViewModel"
x:TypeArguments="vm:IconTintColorBehaviorViewModel"
x:Name="this">
x:TypeArguments="vm:IconTintColorBehaviorViewModel">
<ScrollView>
<Grid
Padding="20"
Expand Down Expand Up @@ -73,23 +72,23 @@
Source="shield.png" />

<ImageButton
x:Name="UpdateImage"
Grid.Row="4"
Grid.Column="1"
Clicked="ChangeSource_Clicked"
Source="shield.png">
Command="{Binding ToggleImageButtonCommand, Mode=OneTime}"
Source="{Binding ToggleableImageSource}">
<ImageButton.Behaviors>
<mct:IconTintColorBehavior TintColor="Red" />
</ImageButton.Behaviors>
</ImageButton>

<ImageButton
x:Name="ToggleColorImageButton"
Grid.Row="4"
Grid.Column="2"
Command="{Binding ChangeCommand}"
Command="{Binding ChangeColorCommand}"
Source="shield.png">
<ImageButton.Behaviors>
<mct:IconTintColorBehavior TintColor="{Binding Source={x:Reference this}, Path=BindingContext.ChangedColor}" />
<mct:IconTintColorBehavior TintColor="{Binding Source={x:Reference ToggleColorImageButton}, Path=BindingContext.ToggleableIconTintColor}" />
</ImageButton.Behaviors>
</ImageButton>

Expand All @@ -98,13 +97,13 @@
Grid.Column="2"
FontSize="Micro"
FontAttributes="Italic"
Text="The ImageButton above uses Binding in order to change the TintColor. Click it!"/>
Text="This ImageButton uses Bindings to change the TintColor. Click it!"/>
<Label
Grid.Row="5"
Grid.Column="1"
FontSize="Micro"
FontAttributes="Italic"
Text="The ImageButton above updates the image source. Click it!"/>
Text="This ImageButton uses Bindings to change the ImageSource. Click it!"/>

</Grid>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,4 @@ public IconTintColorBehaviorPage(IconTintColorBehaviorViewModel iconTintColorBeh
{
InitializeComponent();
}

void ChangeSource_Clicked(System.Object sender, System.EventArgs e)
{
var imageSource = (ImageButton)sender;
var selectedImage = imageSource.Source as FileImageSource;

if (selectedImage is not null)
{
if (selectedImage.File == "dotnet_bot.png")
{
UpdateImage.Source = "shield.png";
}
else
{
UpdateImage.Source = "dotnet_bot.png";
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,37 @@ namespace CommunityToolkit.Maui.Sample.ViewModels.Behaviors;

public partial class IconTintColorBehaviorViewModel : BaseViewModel
{
const string dotnetBotImageFileName = "dotnet_bot.png";
const string shieldImageFileName = "shield.png";

static readonly IEnumerator<Color?> toggleableColorsEnumerator = new List<Color?> { Colors.Red, Colors.Green, null }.GetEnumerator();

[ObservableProperty]
string toggleableImageSource = shieldImageFileName;

[ObservableProperty]
Color? toggleableIconTintColor = toggleableColorsEnumerator.Current;

[RelayCommand]
void Change()
void ToggleImageButton()
{
IsChanged = !IsChanged;
ToggleableImageSource = ToggleableImageSource switch
{
dotnetBotImageFileName => shieldImageFileName,
shieldImageFileName => dotnetBotImageFileName,
_ => throw new NotSupportedException("Invalid image source")
};
}

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ChangedColor))]
bool isChanged;
[RelayCommand]
void ChangeColor()
{
if (!toggleableColorsEnumerator.MoveNext())
{
toggleableColorsEnumerator.Reset();
toggleableColorsEnumerator.MoveNext();
}

public Color ChangedColor => IsChanged ? Colors.Red : Colors.Green;
ToggleableIconTintColor = toggleableColorsEnumerator.Current;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static void SetImageViewTintColor(ImageView image, Color? color)
if (color is null)
{
image.ClearColorFilter();
color = Colors.Transparent;
return;
}

image.SetColorFilter(new PorterDuffColorFilter(color.ToPlatform(), PorterDuff.Mode.SrcIn ?? throw new InvalidOperationException("PorterDuff.Mode.SrcIn should not be null at runtime.")));
Expand Down