简体   繁体   English

图像不透明度,打开和打开

[英]Image Opacity, turn of and on

I have five images, when you click one of them I want that one to get full opacity while the other only gets half, to show it is the selected one. 我有五个图像,当你点击其中一个时,我希望那个图像获得完全不透明度,而另一个只获得一半,以显示它是选定的图像。

I am using MVVM and generally wondering if I'm doing it the right way 我正在使用MVVM,并且通常想知道我是否以正确的方式进行操作

I was thinking about passing the name of the imagesource binded into a property. 我正在考虑将绑定的图像源的名称传递给属性。

<StackLayout Grid.Row="3" Grid.Column="1" Orientation="Horizontal" Spacing="0">
                            <Image Source="{Binding StatusUnresolved}" HorizontalOptions="Center"
                                   VerticalOptions="Center" HeightRequest="40" Opacity="{Binding StatusUnresolvedOpacity}">
                                <Image.GestureRecognizers>
                                    <!--<TapGestureRecognizer Command="{Binding Source={x:Reference this}, Path=OnStatusTappedCommand}" CommandParameter="{Binding StatusUnresolved}" />-->
                                </Image.GestureRecognizers>
                            </Image>
                        </StackLayout>

The list that turns the string into status later on. 稍后将字符串转换为状态的列表。

  public List<IssueStatusModel> PossibleStatusValues
        {
            get
            {
                var items = new List<IssueStatusModel>
                {
                    new IssueStatusModel("statusUnresolved.png", IssueStatus.Unresolved),
                    new IssueStatusModel("statusInProgress.png", IssueStatus.InProgress),
                    new IssueStatusModel("statusDone.png", IssueStatus.Done)
                };

                return items;
            }
        }

Property for opacity 不透明度的属性

       public double StatusDoneOpacity
        {
            get { return statusDoneOpacity; }
            set
            {
                if (statusDoneOpacity != value)
                {
                    statusDoneOpacity = value;
                    NotifyPropertyChanged(nameof(StatusUnresolvedOpacity));
                }
            }
        }

        public string StatusDone
        {
            get { return "statusDone.png"; }
        }

public void OnStatusTapped(string fileName)
        {
                foreach (IssueStatusModel item in StatusValues)
                {
                    if (item.Name != fileName) continue;
                    Issue.Status = item.Status;
                    StatusChecker();
                    return;
                }
            }

    }

Switch statement Changing all the opacities. 切换语句更改所有不透明度。

    private void StatusChecker()
            {
                switch (Issue.Status)
                {
                    case IssueStatus.Unresolved:
                        StatusUnresolvedOpacity = 1;
                        StatusInProgressOpacity = 0.5;
                        StatusDoneOpacity = 0.5;
                        StatusText = "Unresolved";
                        break;
                    case IssueStatus.InProgress:
                        StatusUnresolvedOpacity = 0.5;
                        StatusInProgressOpacity = 1;
                        StatusDoneOpacity = 0.5;
                        StatusText = "In Progress";
                        break;
                    case IssueStatus.Done:
                        StatusUnresolvedOpacity = 0.5;
                        StatusInProgressOpacity = 0.5;
                        statusDoneOpacity = 1;
                        StatusText = "Done";
                        break;
                }
            }

The way id attack this, if you have multiple images, create an ImageVm and encapsulate any image specific implementation details ie enum State and an IsSelected notification properties. id攻击方式,如果你有多个图像,创建一个ImageVm并封装任何图像特定的实现细节,即枚举StateIsSelected通知属性。 Of course if you only have 1 image this becomes trivially easy and you don't need vms 当然,如果你只有1个图像,这变得非常简单,你不需要vms

Use a DataTrigger that binds to an IsSelected MVVM property to set the Opacity and state if you need to change the image source. 使用DataTrigger绑定到一个IsSelected MVVM属性设置Opacity和状态,如果您需要更改图像源。 Obviously on click you will need to set the IsSelected Property and deselect the other VMs 显然,在单击时,您需要设置IsSelected属性并取消选择其他VMs

Example of DataTrigger for IsSelected DataTriggerIsSelected

<Image Grid.Column="2" Stretch="None">
    <Image.Style>
        <Style TargetType="{x:Type Image}">
            <Setter Property="Opacity" Value="0.5" />
            <Style.Triggers>
                <DataTrigger Value="True" Binding="{Binding IsSelected}">
                    <Setter Property="Opacity" Value="0.5"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>

Update 更新

You CAN use triggers with enums, and you can use a tap recognizers to fire commands in your main viewmodals. 您可以在枚举中使用触发器,并且可以使用点击识别器在主视图模式中触发命令。 also commands can take parameters as well. 命令也可以带参数。

Its probably better (knowing what you have described in the comments) to just make a State and Severity enum and bind to it. 它可能更好(知道你在评论中描述了什么)只是使状态和严重性枚举并绑定它。 and set the State and Severity via a command by a gesture. 并通过手势命令设置状态和严重性。

Then you could just make a Trigger for each Image to change the Opacity for each image on the various state and severity. 然后,您可以为每个图像创建一个触发器,以更改各种状态和严重性上每个图像的不透明度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM