wpf组合框和mvvm模式

时间:2019-01-30 19:07:07

标签: c# wpf combobox

我一直在尝试将组合框与列表和selectedvalue绑定,以便在用户进入屏幕时显示在UI上。

不起作用: SelectedValue不出现

用户界面:

<ComboBox Style="{StaticResource ComboBoxStyle}"
                                     Grid.Row="1"                                         
                                     Width="219"                                         
                                     VerticalAlignment="Center"
                                     HorizontalAlignment="Left"
                                     SelectedItem="{Binding SValue,Mode=TwoWay}"
                                     ItemsSource="{Binding SList,Mode=TwoWay}"
                                     SelectedValue="{Binding SValue,Mode=TwoWay}"
                                     DisplayMemberPath="Value"
                                     SelectedValuePath="Value"                                         
                                     Margin="0,8,0,7" Grid.ColumnSpan="2"/>

ViewModel:

public List<StringKeyValue> SList
    {
        get { return _SList; }
        set { SetProperty(ref _SList, value); }
    }

public StringKeyValue SValue
    {
        get { return _SValue; }
        set { SetProperty(ref _SValue, value); }
    }

private List<StringKeyValue> _SList;

    private StringKeyValue _SValue;


SList.Add(new StringKeyValue() { Key = "00", Value = "--" });
SList.Add(new StringKeyValue() { Key = "01", Value = "Test Value" });

SList.Add(new StringKeyValue() { Key = "02", Value = "Test Value2" });
            SValue = SList[1];

下拉区域中没有显示任何值,只是空白 单击下拉菜单时,我可以在列表中看到值:

  1. -
  2. 测试值(此值由于绑定到SelectedItem而突出显示)
  3. 测试值2

如果有人可以帮助我,谢谢

0 个答案:

没有答案