【问题标题】:Silverlight: ComboboxItem text not updated when binding changesSilverlight:绑定更改时未更新 ComboboxItem 文本
【发布时间】:2014-03-21 13:33:06
【问题描述】:

我有一个问题,当我更新 Comboboxitem 上的文本时,它不会立即反映在 UI 上。必须单击组合框以显示项目(具有正确的文本)。任何想法为什么?请注意,这个确切的代码在 WPF 中完美运行

定义要显示的字符串的属性

public string NormallyOpenString
{
    get
    {
        if (this.IsInput)
        {
            return "High";
        }
        else if (this.IsRelay)
        {
            return "Open";
        }
        else
        {
            return "Open (High)";
        }
    }
}

像这样绑定到组合框

<ComboBox SelectedIndex="{Binding Normally, Mode=TwoWay}" >
    <ComboBoxItem Content="{Binding NormallyOpenString}"  />
    <ComboBoxItem Content="{Binding NormallyClosedString}" />
</ComboBox>

当另一个组合框更改时,我想更新文本,因为它更改了 IsInput / IsRelay 是什么。我通过NotifyPropertyChanged 这样做

this.NotifyPropertyChanged("NormallyOpenString");
this.NotifyPropertyChanged("NormallyClosedOpenString");
this.NotifyPropertyChanged("Normally");

【问题讨论】:

    标签: c# wpf silverlight combobox


    【解决方案1】:

    我从来没有这样做过,所以我不能保证。这就是我进行属性更改通知的方式:

    public class MyViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }
    
        private string normallyOpenString = "I'm an open string!";
        public string NormallyOpenString
        {
            get { return normallyOpenString; }
            set
            {
                normallyOpenString = value;
                RaisePropertyChanged("NormallyOpenString");
            }
        }        
    }
    

    所以现在,每当有人调用您的 setter 时,绑定到您的属性的任何内容都会更新。因此,如果它是从一个绑定中设置的,那么所有其他绑定到它的绑定都将被更新。

    【讨论】:

      【解决方案2】:

      我认为您应该使用 SelectedItem 属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-13
        • 2011-04-27
        • 2020-02-06
        相关资源
        最近更新 更多