Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Silverlight:ComboboxItem文本在绑定更改时未更新_C#_Wpf_Silverlight_Combobox - Fatal编程技术网

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

C# Silverlight:ComboboxItem文本在绑定更改时未更新,c#,wpf,silverlight,combobox,C#,Wpf,Silverlight,Combobox,我有一个问题,当我更新Comboboxitem上的文本时,它不会立即反映在UI上。必须单击组合框才能显示具有正确文本的项目。你知道为什么吗?请注意,这个精确的代码在WPF中非常有效 定义要显示的字符串的属性 public string NormallyOpenString { get { if (this.IsInput) { return "High"; } else if (this.Is

我有一个问题,当我更新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>

我从来没有那样做过,所以我不能担保。我是这样做的属性更改通知:

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,绑定到您的属性的任何内容都将被更新。因此,如果它是从一个绑定中设置的,那么也绑定到它的所有其他绑定都将被更新。

我认为您应该使用SelectedItem属性

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