【问题标题】:Trying to use Datatrigger with enum and getting enum "..does not exist in namespace"尝试将 Datatrigger 与枚举一起使用并获取枚举“..在命名空间中不存在”
【发布时间】:2016-07-15 16:09:40
【问题描述】:

我正在使用 WPF 并尝试根据定义为枚举的属性更改我的图像源

xaml 如下所示:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:LogoCam;assembly="
    x:Class="LogoCam.MainWindow"
    Title="Logo Cam" Height="600" Width="600"
    Background="Gainsboro"
    SizeChanged="MainWindow_SizeChanged"
    WindowState="Maximized"
    MinHeight="600"
    MinWidth="600"
    Icon="Resources/millcam.ico"
    WindowStartupLocation="CenterScreen"
    Closed="MainWindow_Closed"
    >

我想根据枚举值更改图像

<Image x:Name="leftImage" Width="180" Height="42" MouseLeftButtonDown="Image_MouseLeftButtonDown">
    <Image.Style>
            <Style TargetType="{x:Type Image}">
                <Setter Property="Source" Value="Resources/Play.png" />
                <Style.Triggers>
                    <DataTrigger Value="{x:Static local:Modes.Playing}" Binding="{Binding Path=ActionMode}" >
                        <Setter Property="Source" Value="Resources/Stop.png"/>
                    </DataTrigger>
                    <DataTrigger Value="Modes.Stopped" Binding="{Binding Path=ActionMode}" >
                        <Setter Property="Source" Value="Resources/Play.png"/>
                    </DataTrigger>
                    <DataTrigger Value="Modes.SnapShot" Binding="{Binding Path=ActionMode}" >
                        <Setter Property="Source" Value="Resources/Back.png"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
    </Image.Style>
 </Image>

查看我在 xaml 中尝试使用它时遇到的错误“名称“模式”不在命名空间 'clr-namespace:LogoCam;assembly=' 中存在:

Xaml.cs 看起来像这样

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.ComponentModel;


namespace LogoCam
{

    public partial class MainWindow : System.Windows.Window, INotifyPropertyChanged
        {

            public enum Modes { Playing, Stopped, SnapShot };

            private Modes  mode = Modes.Stopped;

            public Modes ActionMode
            {
                private set
                {
                    mode = value;
                    OnPropertyChanged("ActionMode");
                }
                get
                {
                    return mode;
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;

            public MainWindow()
            {
                InitializeComponent();
                this.DataContext = this;

            }

            private void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            }
    }
}

为什么在 xaml 中找不到我的枚举?

感谢您的帮助。

【问题讨论】:

    标签: c# .net wpf xaml enums


    【解决方案1】:

    它不在命名空间中,它在你的类中,因此:

    {x:Static local:MainWindow+Modes.Playing}
    

    我避免在类中定义枚举的原因之一。

    【讨论】:

    • 我得到了同样的结果:名称“MainWindow+Modes”不存在于命名空间“clr-namespace:LogoCam;assembly=".
    • 那么local实际上并没有指向正确的命名空间。
    • 我还尝试在发布之前将枚举移入和移出 cals。它没有工作
    • 我不明白 namspace 是 LogoCam。 Local 指向 LogoCam。我错过了什么?
    • 我不知道,您甚至没有发布显示文件名称空间的整个代码,也没有提到这是否是正在执行的程序集。您是否尝试过运行该应用程序?
    猜你喜欢
    • 2011-10-28
    • 2014-10-25
    • 2012-10-18
    • 2012-09-08
    • 2014-09-07
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多