简体   繁体   English

绑定WPF int和int?

[英]Binding WPF int and int?

I have template column like this. 我有这样的模板列。 When I set property in other column I regenerate columns and invoke UpdateLayout for a grid and after that my combobox doesn't bind property of int but when this property is int? 当我在其他列中设置属性时,我重新生成列并为网格调用UpdateLayout,此后,我的组合框不绑定int属性,但是当此属性为int时? all working ok. 一切正常。 Please help me to explain this behaviour. 请帮助我解释这种行为。

<TemplateColumnMetadata Name="User" Header="User"  MinWidth="150">
    <TemplateColumnMetadata.ColumnTemplate>
        <DataTemplate>
            <ComboBox    
                ItemsSource="{Binding Model.Users}"
                DisplayMemberPath="Name"
                SelectedValuePath="Id"
                SelectedValue="{Binding UserId, UpdateSourceTrigger=PropertyChanged}"       
                IsEnabled="{Binding IsCellEditable}">
            <ComboBox>
        </DataTemplate>
    <TemplateColumnMetadata.ColumnTemplate>           
<TemplateColumnMetadata>

1. This code works 1.此代码有效

public int? UserId
{
    get { return Model.UserId; }
    set
    {
        Model.UserId= value.Value;
    }
}

2. This code doesn't work 2.此代码无效

public int UserId
{
    get { return Model.UserId; }
    set
    {
        Model.UserId= value;
    }
}

The problem occured because null value went from xaml and binding got broken. 发生此问题是因为null值来自xaml,并且绑定已损坏。 So it worked for int? 所以它对int有效吗? but it didn't work for int. 但是它不适用于int。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM