Silverlight的组合框的SelectedValue双向绑定不工作(Silverlight C

2019-09-28 03:22发布

我在我的应用程序使用的许多组合框和所有的人都没有任何问题的工作。 但是,我现在不能发现问题。 我已经设置SelectedValuePath为“标记”属性。 但是属性不改变ComboBox中选定项目后更新。 我已阅读其他StackOverflow的问题,但nontheless帮助。

这是XAML:

的xmlns:虚拟机= “CLR的命名空间:SilverlightApplication1”

<UserControl.DataContext>
    <vms:MainViewModel />
</UserControl.DataContext>

<Grid x:Name="LayoutRoot" Background="White">
    <ComboBox Width="100" VerticalAlignment="Center" FontFamily="Segoe UI"
         Height="30" Margin="0,5,0,0"  HorizontalAlignment="Left" 
         SelectedValue="{Binding SelectedDifStatusComparer, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
         SelectedValuePath="Tag">
        <ComboBox.Items>
            <ComboBoxItem Tag="H" >High</ComboBoxItem>
            <ComboBoxItem Tag="L" >Low</ComboBoxItem>
            <ComboBoxItem Tag="E" >Equal</ComboBoxItem>
        </ComboBox.Items>
    </ComboBox>
</Grid>

这里是视图模型:

public class MainViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private string _selectedDifStatusComparer = "";
        private string SelectedDifStatusComparer
        {
            get { return _selectedDifStatusComparer; }
            set
            {
                _selectedDifStatusComparer = value;
                MessageBox.Show(_selectedDifStatusComparer);
                OnPropertyChanged("SelectedDifStatusComparer");
            }
        }

        public MainViewModel()
        {
            SelectedDifStatusComparer = "E"; // It is working, the MessageBox is apperaing
        }
    }

Answer 1:

你的财产是私有的。 其变更为公开,它应该工作。



Answer 2:

你的财产是私有的。 其变更为公开,它应该工作。

 public class MainViewModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }

            private string _selectedDifStatusComparer = "";
            public string SelectedDifStatusComparer
            {
                get { return _selectedDifStatusComparer; }
                set
                {
                    _selectedDifStatusComparer = value;
                    MessageBox.Show(_selectedDifStatusComparer);
                    OnPropertyChanged("SelectedDifStatusComparer");
                }
            }

            public MainViewModel()
            {
                SelectedDifStatusComparer = "E"; // It is working, the MessageBox is apperaing
            }
        }


文章来源: Silverlight ComboBox SelectedValue TwoWay Binding not working