【发布时间】:2011-05-05 10:11:59
【问题描述】:
我有以下从枚举中获取的组合框:
<ComboBox Name="cmbProductStatus" ItemsSource="{Binding Source={StaticResource statuses}}"
SelectedItem="{Binding Path=Status}" />
请注意,DataContext 是在代码隐藏中设置的。
这甚至不是双向绑定,我有一些 Product.Status 的默认值,但它永远不会被选中。
更新
我被要求输入我的 Status 属性的代码。
public class Product {
//some other propertties
private ProductStatus _status = ProductStatus.NotYetShipped;
public ProductStatus Status { get { return _status; } set { value = _status; } }
}
public enum ProductStatus { NotYetShipped, Shipped };
【问题讨论】:
-
在调试器下运行时查看您的输出窗口,看看它是否告诉您有关绑定到状态的任何信息。
-
您能提供您的 Status 属性的代码吗?如果它无法绑定,我会在输出窗口中期待一些东西。
-
如果您想看到更改显示在您的 GUI 中,您需要实现 INotifyPropertyChanged 并以“状态”作为值引发正确的事件。只要您的 ItemSource 包含枚举,尽管我原以为您会获得默认设置。我的博客上有一些关于绑定的调试技巧,您可能会觉得有用。如果您有兴趣,请点击我的名字 - 那里有一个链接。
-
@Russell 感谢您提供调试提示。我的 DataItem 似乎为空。我认为这是因为 ItemSource 设置为 StaticResource,而父容器的 DataContext 设置在代码隐藏中。