【发布时间】: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 中找不到我的枚举?
感谢您的帮助。
【问题讨论】: