将SpeechStateEnumeration绑定到TextBlock

时间:2013-06-04 16:20:05

标签: c# .net wpf xaml

我们如何将枚举绑定到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将其传达给用户?

1 个答案:

答案 0 :(得分:1)

它没有获取绑定的原因是因为您无法绑定到fields。你需要至少有auto-property来实际绑定。

以下对我来说很好:

代码背后:

public SpeechStateEnumeration SpeechState { get; set; }

public Window1()
{
    InitializeComponent();
    SpeechState = SpeechStateEnumeration.Listening;
    DataContext = this;
}

XAML:

<TextBlock Text="{Binding SpeechState}" />