Silverlight组合框SelectedValue双向绑定不起作用 [英] Silverlight ComboBox SelectedValue TwoWay Binding not working

查看:18
本文介绍了Silverlight组合框SelectedValue双向绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用了许多ComboBox,它们都工作正常,没有任何问题。但是,我现在找不到问题所在。我已将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
        }
    }

推荐答案

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

这篇关于Silverlight组合框SelectedValue双向绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆