WPF - ItemsControl触发Setter并覆盖空值

时间:2018-02-15 22:22:27

标签: c# wpf data-binding prism

我正在使用Prism在MVVM中工作,我正在努力将TimeEditor绑定到可空的TimeSpan。每次TwoWay绑定到我的值时,它都会覆盖空值并将其更改为00:00:00。只要我将其更改为OneWay,它就可以正常工作并保持null值。

XAML

           <ItemsControl ItemsSource="{Binding Path=CompanyHoursList, Mode=OneWay}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Vertical"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Border Margin="0 3" BorderBrush="#444" BorderThickness="0 0 0 1" Padding="0 0 0 5">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="70"></ColumnDefinition>
                                    <ColumnDefinition Width="100"></ColumnDefinition>
                                    <ColumnDefinition Width="200"></ColumnDefinition>
                                    <ColumnDefinition Width="100"></ColumnDefinition>
                                </Grid.ColumnDefinitions>

                                <TextBlock Grid.Column="0" Text="{Binding Path=Day}" VerticalAlignment="Center"></TextBlock>
                                <c1:C1TimeEditor Grid.Column="1" Value="{Binding Path=CloseTime, Mode=TwoWay}" AllowNull="True"></c1:C1TimeEditor>
                            </Grid>
                        </Border>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

CompanyHours.cs

 public class CompanyOperationHours : BindableBase
    {
        private DayOfWeek _day;
        private TimeSpan? _openTime;
        private TimeSpan? _closeTime;

        public DayOfWeek Day
        {
            get { return _day; }
            set
            {
                SetProperty(ref _day, value);
            }
        }

        public TimeSpan? OpenTime
        {
            get { return _openTime; }
            set
            {
                SetProperty(ref _openTime, value);
            }
        }

        public TimeSpan? CloseTime
        {
            get { return _closeTime; }
            set
            {
                SetProperty(ref _closeTime, value);
            }
        }

        public CompanyOperationHours(DayOfWeek day)
        {
            Day = day;
        }
  }

这让我绝对疯狂,如果有人能提供一些见解,我会很高兴。如果我将TimeEditor拉出ItemsControl并将其放在一个随机位置,将它绑定到列表中的特定项目和相同的属性,它可以正常工作。只有当我将其添加到ItemsControlTwoWay绑定它时,它才会更改null值并显示12:00:00 AM而不是null。

0 个答案:

没有答案