c# - 如何将枚举对象绑定(bind)到按钮的背景?

标签 c# wpf dictionary binding background-color

我想将按钮的背景颜色绑定(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/

相关文章:

python - python 如何为 GraphViz 编写一个点文件,要求将某些边染成红色?

c# - xaml DataGrid 绑定(bind)到字典中的字典中的字典

c# - 如何将裁剪编码从后面的代码迁移到查看模型 wpf

c# - 如何在 C# 中使用反射获取类型的完全限定名称

c# - 具有实时整形/分组的列表框 - 如何在项目重新分组时保持选择?

c# - 单击后禁用按钮,同时维护 CausesValidation 和 OnClick-Method

wpf - 当文本不再适合一行时,如何配置 TextBox 控件以自动垂直调整自身大小?

c# - WPF 中的 ItemsControl 是否有一个好的解决方案,可以通过垂直拖放对其元素重新排序?

wpf - XAML 智能感知不适用于引用的程序集资源

python - 比较字典键、值与嵌套列表元素 - Python