我想将按钮的背景颜色绑定(bind)到枚举。我想知道是否有一个枚举对象可以保存多个值,例如状态和颜色。我想避免两个可能不同步的枚举。这是我想相互集成的两个枚举。
enum StateValue { Player, Wall, Box }
enum StateColor { Colors.Red, Colors.Grey, Colors.Brown }
然后我需要为 XAML 按钮创建一个绑定(bind)。
<Button Content="Player" Background="{Binding Source=...?}" />
也许,像下面这样的字典会有所帮助。但是我仍然不知道绑定(bind)需要如何编写。
public Dictionary<StateValue, Color> stateValueColor =
new Dictionary<ElementState, Color>()
{
{ StateValue.Player, Colors.Red },
{ StateValue.Wall, Colors.Grey },
{ StateValue.Box, Colors.Brown }
};
最佳答案
要返回正确的类型需要写 return new SolidColorBrush(Colors.Red);
在 StateValueColorConverter
.这解决了丢失背景颜色的问题。
关于c# - 如何将枚举对象绑定(bind)到按钮的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4129270/