绑定到UWP ComboBox

时间:2016-12-20 22:17:05

标签: uwp uwp-xaml template10

在UWP / Template10应用程序中,我们有一个绑定到ViewModel属性的ComboBox。 ViewModel属性的值在OnNavigatedToAsync中设置。如果我们删除ComboBox绑定,则适当地设置ViewModel中的属性。但是如果ComboBox绑定到该属性,则ViewModel属性保持为null。

XAML看起来像这样

<ComboBox 
    Name="JobTypeComboBox" 
    ItemsSource="{x:Bind JobViewModel.JobTypes}" 
    SelectedItem="{x:Bind JobViewModel.JobType,Mode=TwoWay,Converter={StaticResource ChangeTypeConverter}}"/>

ViewModel看起来像这样

JobType _JobType = default(JobType);
public JobType JobType { get { return _JobType; } set { Set(ref _JobType, value); } }
public ObservableCollection<JobType> JobTypes = new ObservableCollection<JobType>(JobTypeService.GetJobTypes());

public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> state)
    {
            this.JobType = job.JobType;

编辑1:转换器看起来像这样

public class ChangeTypeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (targetType.IsConstructedGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
        {
            if (value == null)
            {
                return null;
            }
            targetType = Nullable.GetUnderlyingType(targetType);
        }

        if (value == null && targetType.GetTypeInfo().IsValueType)
            return Activator.CreateInstance(targetType);

        if (targetType.IsInstanceOfType(value))
            return value;

        return System.Convert.ChangeType(value, targetType);
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return Convert(value, targetType, parameter, language);
    }
}

在执行行this.JobType = job.JobType;时,有6个调用转换器:

使用正确的JobType值调用Convert并返回正确的值。

使用value = null调用ConvertBack并返回null;

使用value = null调用Convert并返回null;

使用value = null调用Convert并返回null;

使用value = null调用ConvertBack并返回null;

使用value = null调用Convert并返回null;

所以看起来绑定是将值反弹回ConvertBack为null。

正确返回

Job.JobType。如果从JobTypeComboBox移除了绑定,则this.jobType已正确设置。将绑定添加到JobTypeComboBox后,this.JobType仍为空。

如果this.jobType绑定到JobTypeComboBox,我如何设置read()的值?

1 个答案:

答案 0 :(得分:0)

好吧,使用这个视图模型:

sendIntent.setType("text/html");
    sendIntent.setData( Uri.parse( "mailto:"+receivers.get( 0 ) ) );
    sendIntent.putExtra( Intent.EXTRA_EMAIL, "contact@myemail.com" );
    sendIntent.putExtra( Intent.EXTRA_SUBJECT, getSubject() );
    if( Build.VERSION.SDK_INT >= 24 ) {
        sendIntent.putExtra(Intent.EXTRA_TEXT, message+"\n"+Html.fromHtml("<html><body bgcolor='#ff0000'><table><tr><td bgcolor=\"#ff0000\">aaa</td><td>aaa</td></tr></table></body></html>", Html.FROM_HTML_MODE_LEGACY));
    }
    else{
        sendIntent.putExtra(Intent.EXTRA_TEXT, message+"\n"+Html.fromHtml("<html><body bgcolor='#ff0000'><table><tr><td bgcolor=\"#ff0000\">aaa</td><td>aaa</td></tr></table></body></html>" ) );
    }

这个XAML:

public class MainPageViewModel : ViewModelBase
{
    public MainPageViewModel()
    {
        ComboBoxItem = ComboBoxItems.First();
    }

    string _ComboBoxItem = default(string);
    public string ComboBoxItem { get { return _ComboBoxItem; } set { Set(ref _ComboBoxItem, value); } }

    public ObservableCollection<string> ComboBoxItems { get; } = new ObservableCollection<string>(new[] { "1", "2", "3", "4" });
}

我实现了这一点:

enter image description here

感谢您使用模板10 - 尽管我认为这与T10无关。 : - )

相关问题