Привязка не заполняет значения в поле со списком - PullRequest
0 голосов
/ 23 апреля 2019

Я могу заполнить список в коде позади, но значения не привязываются к списку в 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;

        }

Я ожидаю, что поле со списком будет заполнено определенным выше списком строк.

Вот код XAML:

<ComboBox Grid.Column="2" x:Name="propvar" HorizontalAlignment="Center"  VerticalAlignment="Center" Height="30" Width="100" Margin="5,5,5,5"   IsTextSearchEnabled="True"  FontSize="11"
ItemsSource="{Binding AllProperties}" SelectionChanged="Var_SelectionChanged"
SelectedItem="{Binding Propselected, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />

Я не совсем понимаю, почему значения не отображаются в выпадающем списке.

1 Ответ

0 голосов
/ 23 апреля 2019

Попробуйте это:

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
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...