Comobox Windows 10 MVVM Light

本文关键字:Light MVVM Windows Comobox | 更新日期: 2023-09-27 18:27:44

我正在尝试将值绑定到Windows 10通用应用程序中的组合框。我正在使用MVVM Light。我可以正确绑定除组合框之外的所有值。项目从未显示。我不确定我错过了什么。

 <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作为可观察集合来绑定到组合框。

Comobox Windows 10 MVVM Light

视图模型上没有AvailableOptions属性,只有Options

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