Comobox Windows 10 MVVM Light

时间:2015-12-22 23:11:02

标签: c# mvvm windows-10 win-universal-app

我正在尝试将值绑定到Windows 10 Universal App中的Combobox。我正在使用MVVM Light。除了Combo Box之外,我可以正确绑定所有值。这些项目从未显示过。我不确定我错过了什么。

 <Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:BigappleSP.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ViewModels="using:BigappleSP.ViewModels"
x:Class="BigappleSP.Views.ContactusPage"
mc:Ignorable="d" DataContext="{Binding ContactusViewModdel, Source={StaticResource Locator}}">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <RelativePanel FlowDirection="LeftToRight" Height="640" VerticalAlignment="Bottom">
        <TextBox x:Name="txtName" MinWidth="320" 
            PlaceholderText="Name" InputScope="PersonalFullName"
            RelativePanel.AlignHorizontalCenterWithPanel="True"
            Text="{Binding ContactusViewModel.Name, Mode=TwoWay}"
            Margin="10,10"/>

        <TextBox x:Name="txtEmail" MinWidth="320"
            PlaceholderText="Email"
            RelativePanel.AlignHorizontalCenterWithPanel="True"
            RelativePanel.Below="txtName"
            Margin="10,10" 
            Text="{Binding ContactusViewModel.Email, Mode=TwoWay}"
            InputScope="EmailSmtpAddress" />

        <TextBox x:Name="txtPhone" MinWidth="320"
            PlaceholderText="Phone Number"
            RelativePanel.AlignHorizontalCenterWithPanel="True"
            RelativePanel.Below="txtEmail" Margin="10,10"
            InputScope="TelephoneNumber" 
            Text="{Binding ContactusViewModel.PhoneNumber, Mode=TwoWay}"/>


        <ComboBox x:Name="lstOptions" MinWidth="320" MinHeight="35"
            RelativePanel.AlignHorizontalCenterWithPanel="True"
            RelativePanel.Below="txtPhone" Margin="10,10"                      
            ItemsSource="{Binding Path=ContactusViewModel.AvailableOptions, Mode=TwoWay}" 
            PlaceholderText="How can we help you" 
            IsSynchronizedWithCurrentItem="False"
            DisplayMemberPath="Title">

        </ComboBox>

        <TextBox x:Name="txtMessage" MinWidth="320" MinHeight="150"
            RelativePanel.AlignHorizontalCenterWithPanel="True"
            RelativePanel.Below="lstOptions"
            InputScope="Text" Margin="10,10" 
            Text="{Binding ContactusViewModel.Message, Mode=TwoWay}"/>


    </RelativePanel>

</Grid>

查看模型

public class ContactusViewModel : Template10.Mvvm.ViewModelBase
{

    public string Name { get; set; }

    public string Email { get; set; }

    public int PhoneNumber { get; set; }



    private ObservableCollection<Options> _options = new ObservableCollection<Models.Options>();

    public ObservableCollection<Options> Options
    {
        get
        {
            if (_options == null || _options.Count <= 0)
            {
                _options.Add(new Options() { Id = 1, Title = "Development" });
                _options.Add(new Options() { Id = 2, Title = "Training" });
                _options.Add(new Options() { Id = 3, Title = "Consulting" });

            }
            return _options;
        }
        set
        {
            _options = value;
        }
    }



    public string Message { get; set; }
    // constructor

    public ContactusViewModel()
    {

        _options.Add(new Options() { Id = 1, Title = "Development" });
        _options.Add(new Options() { Id = 2, Title = "Training" });
        _options.Add(new Options() { Id = 3, Title = "Consulting" });
       // Options = _options;

    }

    public override void OnNavigatedTo(object parameter, NavigationMode mode, IDictionary<string, object> state)
    {
        base.OnNavigatedTo(parameter, mode, state);
    }

}

我使用Options作为可观察集合来绑定到Combo Box。

1 个答案:

答案 0 :(得分:0)

您的视图模型中没有AvailableOptions属性,只有Options

您还需要在视图模型中为所选选项提供另一个属性,并将其绑定到组合框的SelectedItem属性。