DataTrigger work with Converter seems have BUG!!!

Requirement Description:

I'd like to change the source of a image by using "Data Trigger" , but when I use it with "Converter" , it doesn't work as expected.

Code:

CustomImage

public class CustomImage : Image
    {

        public CustomImage()
        {
            PropertyChanged += OnPropertyChanged;
        }

        private void OnPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Source")
            {
                var fileSource = (Source as FileImageSource);
            }
        }
    }

Converter

public class IconSourceConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (parameter != null)
        {
            string par = parameter as string;
            if (par == "Selected")
            {
                return "image_one.png";
            }
            else
            {
                return "image_one_selected.png";
            }
        }
        else
        {
            return "image_one.png";
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }

    private enum ConverterParameter
    {
        NoSelected, Selected

    }
}

XamlCodeOne:

<control:CustomImage  WidthRequest="16"   >
                <control:CustomImage.Triggers>

                    <DataTrigger TargetType="control:CustomImage" Binding="{Binding IsVisible}" Value="False">
                   <Setter Property="Source" Value="{Binding PageType, Converter={StaticResource PageTypeToIconSource} , ConverterParameter='Selected' }" />
                    </DataTrigger>

                    <DataTrigger TargetType="control:CustomImage" Binding="{Binding IsVisible}" Value="True">
                                      <Setter Property="Source" Value="{Binding PageType, Converter={StaticResource PageTypeToIconSource} , ConverterParameter='NoSelected' }" />
                    </DataTrigger>

                </control:CustomImage.Triggers>
            </control:CustomImage>

XamlCodeTwo:

    <control:CustomImage  WidthRequest="16"   >
                    <control:CustomImage.Triggers>

                        <DataTrigger TargetType="control:CustomImage" Binding="{Binding IsVisible}" Value="False">
                            <Setter Property="Source" Value="image_one.png" />
                        </DataTrigger>

                        <DataTrigger TargetType="control:CustomImage" Binding="{Binding IsVisible}" Value="True">
                            <Setter Property="Source" Value="image_one_selected.png" />
                        </DataTrigger>

                    </control:CustomImage.Triggers>
                </control:CustomImage>

Result:

When I use XamlCodeTwo , it works well . But when I use XamlCodeOne , it doesn't work as expected and it toggles "OnPropertyChanged"(in CustomImage) several times . As I know , it should only be toggled when the value of DataTrigger change.

Question:

Why I use XamlCodeOne can not work and why it toggles "OnPropertyChanged" (in CustomImage) several times?

Answers

  • Tracy320Tracy320 Member ✭✭

    Well, if it is true, you can post an issue to xamarin group.

Sign In or Register to comment.