【问题标题】:Silverlight ComboBox SelectedValue TwoWay Binding not workingSilverlight ComboBox SelectedValue 双向绑定不起作用
【发布时间】:2013-07-26 05:29:11
【问题描述】:

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

是xaml:

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

【问题讨论】:

  • 请检查您的输出窗口是否存在绑定错误或在运行时使用 Snoop 来检查您的绑定
  • @blindmeis 成功了,我只是忘了公开财产。

标签: c# .net silverlight-4.0


【解决方案1】:

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

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

【讨论】:

  • 谢谢。但我已经在 6 天前接受了另一个答案 :)
【解决方案2】:

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

【讨论】:

  • 谢谢。我不敢相信。 :) 看来,我累了。
猜你喜欢
  • 1970-01-01
  • 2011-09-06
  • 2011-09-17
  • 2011-06-23
  • 2010-10-07
  • 1970-01-01
  • 2012-02-14
相关资源
最近更新 更多