简体   繁体   English

ComboBox选定的项目绑定不显示初始值-然后工作正常

[英]ComboBox selected item binding not showing the initial value - then working OK

I have a weird problem with something simple I suppose. 我想简单的事情有一个奇怪的问题。 I have a combobox with two bindings set up - one for ItemsSource and another for SelectedItem. 我有一个设置了两个绑定的组合框-一个用于ItemsSource,另一个用于SelectedItem。
The selected item is not working on initial startup, but then it works OK. 所选项目在首次启动时不起作用,但随后工作正常。 Output does not indicate any binding problems, I have also set up a TextBlock with the same binding to see if it works - and it does. 输出没有指示任何绑定问题,我还设置了具有相同绑定的TextBlock来查看它是否起作用-并且它确实可以。

Here's the code 这是代码

  <ComboBox IsSynchronizedWithCurrentItem="True" IsEditable="False"
                          Name="ProgramsCollectionComboBox"
                          SelectedItem="{Binding ElementName=ThisUc,
                                                 Path=SelectedProgram}"
                          ItemsSource="{Binding ElementName=ThisUc,
                                                Path=ProgramsCollection}">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}" />
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>

                <TextBlock Text="{Binding ElementName=ThisUc, 
                     Path=SelectedProgram.Name, Mode=TwoWay}" />

The property: 物业:

        private Program _selectedProgram;
        public Program SelectedProgram
        {
            get
            {
                if (_selectedProgram == null)
                {
                        _selectedProgram = new Program(Settings.Default.SelectedProgramPath);
                }
                return _selectedProgram;

            }
        set
        {
                _selectedProgram = value;
                Settings.Default.SelectedProgramPath = SelectedProgram.PathProgramFolder;
                RaisePropertyChanged("SelectedProgram");
        }
    }

It saves and reads the settings OK, the initial values is shown in the textblock below the combobox, when I change the selected item, the textblock is updated, the settings are changed and everything works fine - except for the fact that on app startup, selected item is not selected. 它会保存并读取设置,确定,初始值显示在组合框下方的文本框中,当我更改选定的项目,更新文本块,更改设置且一切正常时-除了在应用启动时,所选项目未选中。

Thanks for help! 感谢帮助!

There are two reasons your initial binding is not working. 您的初始绑定不起作用有两个原因。 First, as Jehof has mentioned himself, is the fact that you're setting your SelectedProgram to an item that is not part of the ProgramsCollection . 首先,正如Jehof所提到的那样,事实是您将SelectedProgram设置为不属于ProgramsCollection

Furthermore, when you are setting the initial value of your SelectedProgram you are doing so in the getter, where PropertyChanged is not invoked and thus the binding will never be aware of that change. 此外,当您设置SelectedProgram的初始值时,是在getter中进行的,在PropertyChanged方法中不会调用PropertyChanged ,因此绑定将永远不会意识到该更改。 You can either invoke PropertyChanged when initializing it in the getter: 您可以在getter中初始化PropertyChanged时调用它:

...
get
{
    if (_selectedProgram == null)
    {
        _selectedProgram = _programsCollection?.FirstOrDefault();
        RaisePropertyChanged("SelectedProgram");
    }
    return _selectedProgram;
}
...

Or even better, set the default value on the private field: 甚至更好的是,在私有字段上设置默认值:

private Program _selectedProgram = _programsCollection?.FirstOrDefault();
...

The getter of your property SelectedProgram should return a value of your ProgrammsCollection and not a new instance if it is null. 属性SelectedProgram的getter应该返回ProgrammsCollection的值,如果它为null,则不要返回新实例。

If the value is not part of the collection that is bound to the combobox it is not displayed. 如果该值不是绑定到组合框的集合的一部分,则不显示该值。

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

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