簡體   English   中英

Silverlight ComboBox SelectedValue TwoWay綁定不起作用

[英]Silverlight ComboBox SelectedValue TwoWay Binding not working

我在我的應用程序中使用了很多ComboBox,所有這些都沒有任何問題。 但是,我現在找不到問題。 我已將SelectedValuePath設置為“Tag”屬性。 但是更改ComboBox選定項后屬性不會更新。 我已閱讀其他StackOverflow問題,但仍然有所幫助。

它是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>

這是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
        }
    }

您的財產是私人的。 將其更改為公開,它應該工作。

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

您的財產是私人的。 將其更改為公開,它應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM