简体   繁体   English

如何将绑定到枚举的ComboBox的初始SelectedItem设置为包含该枚举的对象的Value?

[英]How can I set the initial SelectedItem for a ComboBox bound to an enum, to a Value of an object containing that enum?

I currently have a ComboBox that exists inside a ListView. 我目前在ListView中存在一个ComboBox。 That ComboBox's ItemSource is bound to a StaticResource Enum, which populates the ComboBox with the enum values I'd like to offer to the user. 该ComboBox的ItemSource绑定到一个StaticResource枚举,该枚举用我想提供给用户的枚举值填充ComboBox。 The ListView itself is bound to a an Observable Collection of Objects, which have a property associated with the enum I'm trying to present. ListView本身绑定到一个Observable Objects对象,该对象具有与我要呈现的枚举关联的属性。 Most of this is working. 大部分工作正常。

The ComboBox is properly displaying and showcases the values from the Enum. ComboBox正确显示并显示了Enum中的值。 It even Starts populated with the first item from the Enum I created. 它甚至开始填充我创建的Enum中的第一项。 However, I'm trying to get it to show the actual Enum value for the Object that the Listview is presenting, which it's not currently doing. 但是,我试图获取它以显示Listview所呈现的对象的实际Enum值,而当前并未这样做。 I believe there's an issue with what I'm setting my SelectedValue and SelectedValuePaths to but I'm having trouble figuring out the exact issue. 我相信将我的SelectedValue和SelectedValuePaths设置为什么有问题,但是我很难找出确切的问题。

Here's the XAML I'm using for the ComboBox right now. 这是我现在用于ComboBox的XAML。

<DataTemplate>
  <ComboBox ItemSource={Binding Source={StaticResource dataFromStatusesEnum}}
   SelectedValue="{Binding JMessage3, Mode=TwoWay"}"
   SelectedValuePath="Statuses.Status"
  </ComboBox>
</DataTemplate>

Here's the class I'm filling the ObservableCollection that the ListView is bound to. 这是我正在填充ListView绑定到的ObservableCollection的类。

public class Entity
{
    public string Name {get; set;}
    public Statuses Status {get; set;}
    Public Entity()
    {
        this.Name = "Test";
        this.Status = Statuses.Disabled;
    }
}

And here's the Enum I'm binding the ComboBox ItemSource to right now. 这是我现在将ComboBox ItemSource绑定到的枚举。

public enum Statuses
{
    Enabled,
    Disabled,
    Deleted
}

As I understand the XAML I've created, the ListBox is properly Bound to the ItemSource and displaying the information I'd expect. 据我了解我已经创建的XAML,ListBox正确绑定到ItemSource并显示我期望的信息。

The ComboBox is properly bound to the Enum and displaying those in the DropDown as options for the ComboBox. ComboBox已正确绑定到Enum,并在DropDown中将其显示为ComboBox的选项。

I think the SelectedValuePath SHOULD be the Status property of the Entity Class itself and I think the SelectedValue SHOULD be the enum that I'd like this to be set to, but obviously I'm wrong in this assumption. 我认为SelectedValuePath应该是Entity Class本身的Status属性,并且我认为SelectedValue应该是我希望将其设置为枚举的类型,但是显然我在这种假设下是错误的。

Edit: Wanted to add that this is not using the MVVM Pattern currently. 编辑:想补充一点,这是当前不使用MVVM模式。 Something I'm working toward eventually but not implementing just yet. 我正在为最终的目标而努力,但目前尚未实现。

Got this working. 得到了这个工作。 The SelectedValue should've been set to the Status property of the Entity, and SelectedValuePath didn't have to be set to anything. 应该将SelectedValue设置为实体的Status属性,并且不必将SelectedValuePath设置为任何值。 I'm not entirely sure why but it's working. 我不完全确定为什么,但是它能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM