Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# WPF DatePicker Binding

I have a problem with the Binding nullable property to DatePicker. What i have when run my my App first run but when i set date in range from today to 7 days after it work fine. If i pick date after one week then property set to null. week after

public class Pledge
{
    public int ID { get; set; }
    public string ClientName { get; set; }
    public string Passport { get; set; }
    public string IssuedBy { get; set; }
    [Column(TypeName = "Date")]
    public DateTime? Birthday { get; set; }
    [Column(TypeName = "Date")]
    public DateTime? IssuedDate { get; set; }
    public string Registration { get; set; }
    public string Phone { get; set; }
    public string Model { get; set; }
    public string SerialNumber { get; set; }
    [Column(TypeName = "Date")]
    public DateTime? ReceiptDate { get; set; }
    [Column(TypeName = "Date")]
    public DateTime? IssueDate { get; set; }
    public decimal? Result { get; set; }
    public decimal? MoneyForClient { get; set; }
    public int? Procent { get; set; }
}

XAML:

<StackPanel>
                <TextBlock Text="ФИО:" Margin="4"/>
                <TextBox Text="{Binding Path=ClientName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                <TextBlock Text="Номер телефона:" Margin="4"/>
                <xctk:MaskedTextBox Text="{Binding Path=Phone, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Mask="+7 (000) 000-00-00"/>
                <TextBlock Text="Дата рождения:" Margin="4"/>
                <DatePicker Text="{Binding Path=Birthday, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                <TextBlock Text="Паспорт" Margin="4"/>
                <xctk:MaskedTextBox Text="{Binding Path=Passport, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Mask="0000-000000"/>
                <TextBlock Text="Дата выдачи:" Margin="4"/>
                <DatePicker Text="{Binding Path=IssuedDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                <TextBlock Text="Выдан:" Margin="4"/>
                <TextBox Text="{Binding Path=IssuedBy, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap" AcceptsReturn="True" Height="100"/>
                <TextBlock Text="Прописка:" Margin="4"/>
                <TextBox Text="{Binding Path=Registration, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap" AcceptsReturn="True" Height="100"/>
                <TextBlock Text="Модель:" Margin="4"/>
                <TextBox Text="{Binding Path=Model, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                <TextBlock Text="Серийный номер(IMEI):" Margin="4"/>
                <TextBox Text="{Binding Path=SerialNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                <TextBlock Text="Сумма на выдачу:" Margin="4"/>
                <TextBox Text="{Binding Path=MoneyForClient, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                <TextBlock Text="Процент:" Margin="4"/>
                <TextBox Text="{Binding Path=Procent, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                <TextBlock Text="Дата приема:" Margin="4"/>
                <DatePicker Text="{Binding Path=ReceiptDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                <TextBlock Text="Дата выдачи:" Margin="4"/>
                <DatePicker Text="{Binding Path=IssueDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            </StackPanel>
like image 347
D. Malakhov Avatar asked Apr 16 '26 19:04

D. Malakhov


1 Answers

find the problem. I confused the properties. Instead of the "Text" it was necessary to bind to the "Selected date" in DatePicker. Was:

<DatePicker Text="{Binding Path=MoneyForClient, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

Should be:

<DatePicker SelectedDate="{Binding Path=MoneyForClient, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
like image 106
D. Malakhov Avatar answered Apr 18 '26 07:04

D. Malakhov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!