【问题标题】:datatrigger on enum to change image枚举上的数据触发器以更改图像
【发布时间】:2012-12-04 17:14:36
【问题描述】:

我有一个带有固定背景图像的按钮,并希望在其顶部显示一个小的叠加图像。选择哪个覆盖图像取决于相应视图模型的依赖属性 (LapCounterPingStatus)。

这是我目前得到的:

<Button>
    <Grid>
        <Image Stretch="None"> <!-- Background Image -->
            <Image.Style>
                <Style TargetType="{x:Type Image}">
                    <Setter Property="Source" Value="/Images/Pingn.png"/>
                </Style>
            </Image.Style>
        </Image>
        <Image Stretch="None" Panel.ZIndex="1"> <!-- Small Overlay Image -->
            <Image.Style>
                <Style TargetType="{x:Type Image}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=LapCounterPingStatus}" Value="PingStatus.PING_UNKNOWN">
                            <Setter Property="Source" Value="/Images/RefreshOverlayn.png"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=LapCounterPingStatus}" Value="PingStatus.PING_FAILURE">
                            <Setter Property="Source" Value="/Images/ErrorOverlayn.png"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=LapCounterPingStatus}" Value="PingStatus.PING_SUCCESS">
                            <Setter Property="Source" Value="/Images/CheckmarkOverlayn.png"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>
    </Grid>
</Button>

我的视图模型的相关部分

public class ConfigurationViewModel
{
    public enum PingStatus { PING_UNKNOWN, PING_SUCCESS, PING_FAILURE };

    public PingStatus LapCounterPingStatus
    {
        get { return _lapCounterPingStatus; }
        set
        {
            _lapCounterPingStatus = value;
            RaisePropertyChanged(LapCounterPingStatusPropertyName);
        }
    }
}

现在,根本没有显示叠加图像。有什么问题?


更新

我的 IDE 的跟踪窗口显示 System.ArgumentExceptionSystem.FormatException。 问题来源可能是 XAML 中未知类型的枚举PingStatus

【问题讨论】:

标签: c# wpf


【解决方案1】:

你需要做两件事才能让它工作:

1 - 在 XAML 文件的根元素中将 xmlns 引用添加到定义枚举的命名空间:

<UserControl ...
xmlns:my="clr-namespace:YourEnumNamespace;assembly=YourAssembly"> 

2 - 在DataTriggerValue 属性中,使用{x:Static} 形式:

 <DataTrigger Binding="{Binding Path=LapCounterPingStatus}" Value="{x:Static my:PingStatus.PING_UNKNOWN}">

请注意,Enum 类型必须以您在上面定义的 xmlns 前缀作为前缀。

编辑:

如果您的 Enum 在类中声明,则需要使用以下语法:

{x:Static namespace:ClassName+EnumName.EnumValue}

例如:

{x:Static my:ConfigurationViewModel+PingStatus.PING_UNKNOWN}

【讨论】:

  • 我添加了 xmlns 像这样:xmlns:local="clr-namespace:MyCompany.Testbench" 和像 &lt;DataTrigger Binding="{Binding Path=LapCounterPingStatus}" Value="{x:Static local:PingStatus.PING_UNKNOWN}"&gt; 这样的触发器。不,我收到错误 Cannot find the type 'PingStatus'
  • enum PingStatusMyCompany.TestBench.ConfigurationViewModel 类中定义。我必须在某处添加类名吗?
  • 谢谢。我在任何地方都找不到嵌套类型的语法。 “+”语法记录在哪里?我在 MSDN 或我拥有的 WPF 书籍中找不到它。我认为它应该在x:Static Markup Extension,但它不是。
  • @skst + 符号将包含类型与嵌套命名空间区分开来。 Type t = typeof (System.Environment.SpecialFolder); Console.WriteLine (t.FullName); // prints System.Environment+SpecialFolder
【解决方案2】:

WPF + MVVM 的完整示例。

在 MSVC 2017 上测试。

在视图中:

<TextBlock Text="Some text to be colored by an enum">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding StatusIcon}" Value="{x:Static my:StatusIcon.Warning}">
                    <Setter Property="Foreground" Value="Yellow"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding StatusIcon}" Value="{x:Static my:StatusIcon.Error}">
                    <Setter Property="Foreground" Value="Red}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

如果使用ReSharper,并且DataContext设置正确,当你在StatusIcon之后点击.时会有智能感知,即它会显示枚举的属性DebugInfo , WarningError.

如果使用 ReSharper,它将建议对 XAML 文件标头中的命名空间进行以下更新(其 很好):

xmlns:my="clr-namespace:Class.Path.MyViewModel;assembly=MyAssembly"

还有 VieModel:

public enum StatusIcon
{
    Debug,
    Info,
    Warning,
    Error
}

public class MyViewModel
{
    public StatusIcon StatusIcon { get; }
}

我们还使用Fody 进行自动绑定。

【讨论】:

  • 你指的是 Fody 的 PropertyChanged 项目吗?
【解决方案3】:

您可以简单地将枚举值设置为 DataTrigger 值...在 MSVC 2017 上测试。

<TextBlock Text="Some text to be colored by an enum">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding StatusIcon}" Value="Warning">
                    <Setter Property="Foreground" Value="Yellow"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding StatusIcon}" Value="Error">
                    <Setter Property="Foreground" Value="Red}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

【讨论】:

    猜你喜欢
    • 2014-07-26
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 2017-07-28
    • 2011-06-02
    相关资源
    最近更新 更多