1

我在我的应用程序中使用了许多组合框,它们都可以正常工作。但是,我现在找不到问题。我已将 SelectedValuePath 设置为“Tag”属性。但更改 ComboBox 选定项后属性未更新。我已经阅读了其他 StackOverflow 问题,但仍然有所帮助。

它是 xaml:

xmlns:vms="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>

这是 ViewModel:

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
        }
    }
4

2 回答 2

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
            }
        }
于 2013-08-02T04:15:25.370 回答
1

您的财产是私有的。将其更改为公开,它应该可以工作。

于 2013-07-26T05:35:32.917 回答