Silverlight:绑定更改时未更新ComboboxItem文本

时间:2014-03-21 13:33:06

标签: c# wpf silverlight combobox

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

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

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

哪个绑定到像这样的Combobox

<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");

2 个答案:

答案 0 :(得分:0)

我从来没有这样做过,所以我无法保证。这就是我如何进行财产变更通知:

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");
        }
    }        
}

现在,每当有人打电话给你的二传手,任何绑定到你的财产的东西都会被更新。因此,如果它从一个绑定设置,则所有其他绑定到它的绑定都将被更新。

答案 1 :(得分:0)

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