我们如何将枚举绑定到TextBlock
?
在代码隐藏中,我有:
public enum SpeechStateEnumeration
{
Listening,
Recording,
Dictating,
Working,
Sleeping,
Unresponsive
}
public static SpeechStateEnumeration SpeechState;
button1_Click(object sender, EventArgs e)
{
SpeechState = SpeechStateEnumeration.Sleeping;
}
我的XAML是:
<TextBlock x:Name="Status" Text="{Binding SpeechState}" />
但它不起作用。例如,如果我将SpeechState
设置为“倾听”或“休眠”,如何通过TextBlock
将其传达给用户?
答案 0 :(得分:1)
它没有获取绑定的原因是因为您无法绑定到fields
。你需要至少有auto-property
来实际绑定。
以下对我来说很好:
代码背后:
public SpeechStateEnumeration SpeechState { get; set; }
public Window1()
{
InitializeComponent();
SpeechState = SpeechStateEnumeration.Listening;
DataContext = this;
}
XAML:
<TextBlock Text="{Binding SpeechState}" />