简体   繁体   English

WPF 组合框初始值被所选项目覆盖

[英]WPF Combobox initial value being overridden by selected item

I have the following problem:我有以下问题:

When my view loads the combobox selected value is empty.当我的视图加载时,组合框选择的值为空。

问题

Here is the code for my view:这是我的观点的代码:

<UserControl.Resources>
    ...
    <CollectionViewSource x:Key="SpecialtiesSource" Source="{Binding Path=Specialties}" />
    ...
</UserControl.Resources>

...

<ComboBox Grid.Row="11" Grid.Column="2" Name="Specialty" Style="{StaticResource ComboBoxItem}"
          SelectedIndex="0"
          IsEditable="True"
          TextSearch.TextPath="Name"
          DisplayMemberPath="Name" 
          ItemsSource="{Binding Source={StaticResource SpecialtiesSource}}" 
          SelectedItem="{Binding SelectedSpecialty}"
          SelectedValuePath="Name">
</ComboBox>

Here is my view model:这是我的视图模型:

private List<Specialty> _specialties;
public List<Specialty> Specialties
{
    get
    {
        return _specialties;
    }

    set
    {
            _specialties = value;
            NotifyOfPropertyChange(() => Specialties);
        }
    }

    private Specialty _selectedSpecialty;
    public Specialty SelectedSpecialty
    {
        get
        {
            return _selectedSpecialty;
        }

        set
        {
            if (value == null)
            {
                return;
            }

            _selectedSpecialty = value;
            NotifyOfPropertyChange(() => SelectedSpecialty);
        }
    }

Model:型号:

public class Specialty : IGeneric
{
    public string Code { get; set; }
    public string Name { get; set; }
    public string TabColour { get; set; }
    public string TextColour { get; set; }
    public bool? ActiveFlag { get; set; }
}

public interface IGeneric
{
    string Name { get; set; }
}

When I am debugging the code I can see that my Specialties object is returned with the correct results, because when I click the drop down menu I can then see the all of the results as I would expect.当我调试代码时,我可以看到我的 Specialties 对象返回了正确的结果,因为当我单击下拉菜单时,我可以看到我期望的所有结果。

However immediately after it has loaded this object, I can see my SelectedSpecialty object then being hit which is null.然而,在它加载这个对象之后,我可以立即看到我的 SelectedSpecialty 对象然后被命中,这是空的。 I am guessing this is because of some underlying interaction between when the ItemsSource is loaded and then attempting to set the value for SelectedItem at index position 0?我猜这是因为加载 ItemsSource 和尝试在索引位置 0 处设置 SelectedItem 的值之间存在一些潜在的交互?

Any input or advice on how I can figure out why SelectedItem is being set and how I can stop/set this correctly the the first item of my SpecialtiesSource?关于如何找出 SelectedItem 被设置的原因以及如何正确停止/设置我的 SpecialtiesSource 的第一个项目的任何输入或建议?

I'm also not 100% confident I'm using the SelectedValuePath correctly.我也不是 100% 有信心正确使用 SelectedValuePath。

The bound SelectedItem starts at null.绑定的 SelectedItem 从 null 开始。 Same with ItemsSource.与 ItemsSource 相同。 If you are seeing a list, you must initialize the Specialties somewhere that isn't shown in the question, just set the SelectedSpecialty after that.如果您看到一个列表,则必须在问题中未显示的某处初始化 Specialties,然后设置 SelectedSpecialty。

Or if it is a static list loaded at run time, create the list in the get for Specialties.或者,如果它是在运行时加载的静态列表,请在获取专业中创建列表。 Then you can initialize SelectedSpecialty at runtime to one of the Specialties items.然后您可以在运行时将 SelectedSpecialty 初始化为 Specialties 项目之一。

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

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