组合框 SelectedValue 不更新 textBlock

问题描述

您好,我无法发布所有代码,但使用级联组合框似乎无法将 selectedItem 值作为标签或文本块。 真正的问题是所有其他属性更改的逻辑都按预期工作。

这里是对象类:

 public class NosInterval : BaseModel
    {



        public NosInterval(Core parent) : base(parent)
        {
            InternalValues = new CustomObservableCollection<int>();
        }
        private string _type;
        public string Type
        {
            get
            {
                return _type;
            }
            set
            {
                _type = value;
                OnPropertyChanged(nameof(Type));
            }
        }
        private CustomObservableCollection<int> _InternalValues;
        public CustomObservableCollection<int> InternalValues
        {
            get
            {
                return _InternalValues;
            }
            set
            {
                _InternalValues = value;
                OnPropertyChanged(nameof(InternalValues));
            }
        }

    }

wpf

  <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding SelectednosType.Type }"  ></TextBlock>               
            <Label Content="YAxis Settings :"></Label>
            <ComboBox Name="hgh" ItemsSource="{Binding NosTypes}" displayMemberPath="Type"  SelectedItem="{Binding SelectednosType,Mode=TwoWay}"  IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Left" Width="70" Height="25" VerticalAlignment="Top" Margin="20,0" />
            <ComboBox Name="gh" ItemsSource="{Binding InternalValues}" SelectedValue="{Binding SelectednosInterval,Mode=TwoWay}"  IsSynchronizedWithCurrentItem="True"    HorizontalAlignment="Left" Width="70" Height="25" VerticalAlignment="Top" Margin="20,0"></ComboBox>
        </StackPanel>

让我们仔细看看“SelectednosType”,首先我被迫用 foreach 重新填充另一个集合(这里的命名相同)与对象中的一个使用 straigth 对象中的集合不会填充第二个组合框,我觉得很奇怪

     private NosInterval _SelectednosType;
    public NosInterval SelectednosType
    {
        get { return _SelectednosType; }
        set
        {
            _SelectednosType = value;

            InternalValues.Repopulate(SelectednosType.InternalValues);
         
            Console.WriteLine("Updating Type : " + SelectednosType.Type );          
            OnPropertyChanged(nameof(SelectednosType));
    
        }
    }
 private CustomObservableCollection<NosInterval> _NosTypes;
        public CustomObservableCollection<NosInterval> NosTypes
        {
            get { return _NosTypes; }
            set
            {
                _NosTypes = value; 
                OnPropertyChanged(nameof(NosTypes));
            }
        }

控制台上的输出符合预期

更新类型:周
更新类型:卷

所以 1 - 为什么那些文本块不会用我的 SelectednosType.Type 更新? 2 - 为什么我必须创建另一个集合来填充第二个组合框,而不是直接绑定对象类 NosInterval 中的一个

符合我需要的答案在同一类中设置所有需要的集合 + selecteditem

   public class NosIntervalsDB : BaseModel
    {
        public NosIntervalsDB(Core parent) : base(parent)
        {
            IntervalsCollection = new CustomObservableCollection<NosInterval>();
        }
        public CustomObservableCollection<NosInterval> IntervalsCollection { get; set; }

        private NosInterval _SelectedType;
        public NosInterval SelectedType
        {
            get
            {
                return _SelectedType;
            }
            set
            {
                _SelectedType = value;
                OnPropertyChanged(nameof(SelectedType));
            }
        }

        private int _SelectedValue;
        public int SelectedValue
        {
            get
            {
                return _SelectedValue;
            }
            set
            {
                _SelectedValue = value;
                OnPropertyChanged(nameof(SelectedValue));
            }
        }

    }

    public class NosInterval : BaseModel
    {



        public NosInterval(Core parent) : base(parent)
        {
            InternalValues = new CustomObservableCollection<int>();
            _InternalValues = new CustomObservableCollection<int>();
        }
        private string _type;
        public string Type
        {
            get
            {
                return _type;
            }
            set
            {
                _type = value;
                OnPropertyChanged(nameof(Type));
            }
        }
        private CustomObservableCollection<int> _InternalValues;
        public CustomObservableCollection<int> InternalValues
        {
            get
            {
                return _InternalValues;
            }
            set
            {

                _InternalValues = value;
              
                
                OnPropertyChanged(nameof(InternalValues));

             
            }
        }

    }

然后绑定现在也在文本框中做出反应。

  <StackPanel Orientation="Horizontal">
                    
                        <Label Content="YAxis Settings :"></Label>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding NosTypes.SelectedType.Type,Mode=OneWay}"  ></TextBlock>
                            <TextBlock Text="{Binding NosTypes.SelectedValue }"  ></TextBlock>
                            <Label Content="YAxis Settings :"></Label>
                            <ComboBox Name="cbx_type" ItemsSource="{Binding NosTypes.IntervalsCollection}" displayMemberPath="Type"  SelectedItem="{Binding  NosTypes.SelectedType,0" />
                            <ComboBox Name="cbx_value" ItemsSource="{Binding  SelectedItem.InternalValues,ElementName=hgh}" SelectedValue="{Binding  NosTypes.SelectedValue,0"></ComboBox>
                        </StackPanel>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...