WPF组合框selectedindex显示旧值

时间:2019-03-05 17:11:40

标签: c# wpf

我有一个带有两个不同绑定的组合框。

                <ComboBox Canvas.Left="194" Canvas.Top="101" Width="80" Height="30" FontWeight="Normal" SelectedIndex="0">
                <ComboBox.ItemsSource>
                    <CompositeCollection>
                        <ComboBoxItem Content="{Binding Source={x:Static my:DesignBinding.Instance},Path=AuxCoilWire,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
                        <CollectionContainer Collection="{Binding Source={StaticResource ConfigDataPrimaryWire}, XPath=copperWires/copperWire/add[1]/@value}"/>
                    </CompositeCollection>
                </ComboBox.ItemsSource>
            </ComboBox>

ComboboxItem从另一个窗口获取值,并始终通过propertyChanged方法进行更新

    public class DesignBinding : INotifyPropertyChanged
{
    private static readonly DesignBinding instance = new DesignBinding();
    private DesignBinding() { }

    public static DesignBinding Instance
    {
        get { return instance; }
    }

    private double auxCoilWire;
    public double AuxCoilWire
    {
        get { return auxCoilWire; }
        set { auxCoilWire = value; NotifyPropertyChanged("AuxCoilWire"); }
    }
        public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }

该代码实际上有效,组合框获得了新值,但显示出selectedindex =“ 0”仍然是旧值。 我必须打开下拉菜单来更新值。 然后一切正常,组合框仅显示新值。 我如何立即更新值而不必打开下拉菜单? 我已经搜索了几个小时并尝试过,但是没有找到合适的解决方案:(

2 个答案:

答案 0 :(得分:0)

好的,我有一个非常糟糕的解决方案。我创建了一个加载事件处理程序,然后将IsDropDownOpen属性设置为true,然后再次设置为false。 我找的太久了..

答案 1 :(得分:0)

您的绑定仅适用于 content 是否正确?如果您没有通过GUI选择任何内容,那么您仅在更新下拉菜单。

除非您选择一个项目,否则 selecteditem 属性不会更改。如果是这种情况,您应该调用事件或在代码中强制Combobox.selecteditem。

相关问题