绑定不会填充组合框中的值

时间:2019-04-23 03:02:47

标签: c# xaml data-binding binding

我能够在后面的代码中填充List,但这些值不会绑定到xaml中的组合框

这是示例代码

ActionCache

我希望组合框将填充上面定义的字符串列表。

这是XAML代码:

 public IEnumerable<string> _AllProperties;
 public IEnumerable<string> AllProperties
        {
            get { return _AllProperties; }
            set
            {
                if (_AllProperties == value) return;
                _AllProperties = value;

                OnPropertyChanged("AllProperties");
            }
        }     

 public MainWindow()
        {
            InitializeComponent();
            // Create a list  
            List<string> PropertyValues = new List<string>();
            // Add a range of items  
            string[] values = {  "BlackOilFluid",
            "Boundary",
            "Casing",
            "CheckValve",
            "Choke",
            "Completion",
            "CompletionConingPoint",
            "CompletionModel",
            "CompositionalFluid",
            "Compressor",
            "EngineKeywords",
            "ESP",
            "Expander",
            "FileBasedFluid",
            "Flowline",
            "FluidComponent",
            "GasLiftInjection",
            "GenericBooster",
            "GenericEquipment",
            "GravelPack",
            "HeatExchanger",
            "Injector",
            "IPRBackPressure",
            "IPRDarcy",
            "IPRFetkovitch",
            "IPRForchheimer",
            "IPRHorizontalPI",
            "IPRHydraulicFracture",
            "IPRJones",
            "IPRPIModel",
            "IPRPSSBabuOdeh",
            "IPRSSJoshi",
            "IPRVogel",
            "Junction",
            "Liner",
            "MeasurementPoint",
            "MFLFluid",
            "MultiphaseBooster",
            "MultiplierAdder",
            "NetworkSim",
            "NodalAnalysisOp",
            "OneSubseaBooster",
            "OpenHole",
            "Packer",
            "PCP",
            "PTProfileSim",
            "Pump",
            "PVTFluid",
            "RodPump",
            "SinglephaseSeparator",
            "Sink",
            "SlidingSleeve",
            "Source",
            "Study",
            "SubsurfaceSafetyValve",
            "ThreePhaseSeparator",
            "Tubing",
            "TubingPlug",
            "TwoPhaseSeparator",
            "WaterTempVelocitySurvey",
            "Well",
            "WetGasCompressor" };

            PropertyValues.AddRange(values);

            _AllProperties = PropertyValues;

        }

我不太明白为什么组合框下拉列表中没有显示这些值。

1 个答案:

答案 0 :(得分:0)

尝试一下:

Xaml:

<Window x:Class="TestChipDeleting.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" 
        Height="400" 
        Width="400">

    <StackPanel VerticalAlignment="Center"
                DataContext="{Binding}">

        <ComboBox ItemsSource="{Binding Path=Properties}"
                 IsTextSearchEnabled="True" 
                 FontSize="11"
                 Margin="8"
                 Height="30" 
                 Width="200"
                 HorizontalAlignment="Center"  
                 VerticalAlignment="Center" 
                 SelectionChanged="Var_SelectionChanged"
                 SelectedItem="{Binding Propselected, 
            NotifyOnValidationError=True,
            UpdateSourceTrigger=PropertyChanged,
            ValidatesOnDataErrors=True, 
            ValidatesOnExceptions=True}"/>

    </StackPanel>
</Window>

C#:

namespace Test
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainViewModel();
        }

        private void Var_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }


    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            BindItems();
        }

        private List<string> _properties;

        public List<string> Properties
        {
            get { return _properties; }
            set
            {
                if (value.Equals(_properties))
                    return;
                _properties = value;
                OnPropertyChanged(nameof(Properties));
            }
        }

        private void BindItems()
        {
            string[] items =
            {
                "BlackOilFluid",
                "Boundary",
                "Casing",
                "CheckValve",
                "Choke",
                "Completion",
                "CompletionConingPoint",
                "CompletionModel",
                "CompositionalFluid",
                "Compressor",
                "EngineKeywords",
                "ESP",
                "Expander",
                "FileBasedFluid",
                "Flowline",
                "FluidComponent",
                "GasLiftInjection",
                "GenericBooster",
                "GenericEquipment",
                "GravelPack",
                "HeatExchanger",
                "Injector",
                "IPRBackPressure",
                "IPRDarcy",
                "IPRFetkovitch",
                "IPRForchheimer",
                "IPRHorizontalPI",
                "IPRHydraulicFracture",
                "IPRJones",
                "IPRPIModel",
                "IPRPSSBabuOdeh",
                "IPRSSJoshi",
                "IPRVogel",
                "Junction",
                "Liner",
                "MeasurementPoint",
                "MFLFluid",
                "MultiphaseBooster",
                "MultiplierAdder",
                "NetworkSim",
                "NodalAnalysisOp",
                "OneSubseaBooster",
                "OpenHole",
                "Packer",
                "PCP",
                "PTProfileSim",
                "Pump",
                "PVTFluid",
                "RodPump",
                "SinglephaseSeparator",
                "Sink",
                "SlidingSleeve",
                "Source",
                "Study",
                "SubsurfaceSafetyValve",
                "ThreePhaseSeparator",
                "Tubing",
                "TubingPlug",
                "TwoPhaseSeparator",
                "WaterTempVelocitySurvey",
                "Well",
                "WetGasCompressor"
            };

            Properties = new List<string>(items);
        }

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}