1

我有一个包含一对 DateTime 的视图模型?对象 - 可为空的日期时间。

private DateTime? _xmitdtFrom;
public DateTime? xmitdtFrom
{
    get { return this._xmitdtFrom; }
    set
    {
        this._xmitdtFrom = value;
        notifyPropertyChanged("xmitdtFrom");
    }
}

private DateTime? _xmitdtTo;
public DateTime? xmitdtTo
{
    get { return this._xmitdtTo; }
    set
    {
        this._xmitdtTo = value;
        notifyPropertyChanged("xmitdtTo");
    }
}

xmitdtFrom 日期不能大于 xmitdtFrom 日期,xmitdtTo 日期不能早于 xmitdtFrom 日期,xmitdtTo 日期和 xmitdtFrom 都不能晚于今天。

所以,在标记中我有这个:

<Label Grid.Row="1" Grid.Column="1">
    From:
</Label>
<DatePicker Grid.Row="1" Grid.Column="2" 
        SelectedDate="{Binding xmitdtFrom, Mode=TwoWay}"
        DisplayDateEnd="{Binding xmitdtTo}"
        />
<Label Grid.Row="2" Grid.Column="1">
    Through:
</Label>
<DatePicker Grid.Row="2" Grid.Column="2"
        SelectedDate="{Binding xmitdtTo, Mode=TwoWay}"
        DisplayDateStart="{Binding xmitdtFrom}"
        DisplayDateEnd="{x:Static sys:DateTime.Now}"
        />

这工作正常,除非 xmitdtTo 为空 - 在这种情况下 xmitdtFrom 不受限制,这是一个问题。

我想要的是将 xmitdtFrom 的 DisplayDateEnd 设置为 xmitdtTo(如果它不为空)或 DateTime.Now(如果是)。

我想知道实现这一目标的最干净的方法是什么。

4

3 回答 3

1

I decided to go with another approach entirely.

Instead of messing about with the viewmodel, I created an IfNullConverter, that when used in a binding would pass the bound object, if it was not null, or would pass its ConversionParameter, if it was.

And I used that in binding the from date's DisplayDateEnd - with xmitDTTo as the bound property, and DateTime.Now as the ConversionParameter.

Solves the problem cleanly, entirely within the UI (and this is a UI problem, not a data problem, so I'd prefer a solution that doesn't pollute the viewmodel). And it creates a general purpose functionality, that will be available to use in other similar circumstances.

The converter:

public class IfNullConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return parameter;
        else
            return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

The binding:

<DatePicker Grid.Row="1" Grid.Column="2" 
    SelectedDate="{Binding Path=xmitdtFrom, Mode=TwoWay}"
    DisplayDateEnd="{Binding xmitdtThrough, Converter={StaticResource ifNullConverter}, ConverterParameter={x:Static sys:DateTime.Now}}"
    />
于 2012-10-06T12:46:54.477 回答
0

也许您可以尝试添加另一个可以在 DisplayDateEnd 中绑定的属性。像这样的东西(未经测试):

    private DateTime? _displayDateEnd;
    public DateTime? DisplayDateEnd
    {
        get { return this._displayDateEnd; }
        set
        {
            this._displayDateEnd = value; 
            notifyPropertyChanged("DisplayDateEnd");
        }

从 xmidtTo 设置新属性的值:

    private DateTime? _xmitdtTo; 
    public DateTime? xmitdtTo 
    { 
        get { return this._xmitdtTo; }
        set 
        { 
            this._xmitdtTo = value;

            if (_xmitdtTo == null)
                DisplayDateEnd = _xmitdtTo;
            else
                DisplayDateEnd = DateTime.Now();

            notifyPropertyChanged("DisplayDateEnd"); 
            notifyPropertyChanged("xmitdtTo"); 
        } 
    }

您更新的 xaml:

<DatePicker ... DisplayDateEnd="{Binding DisplayDateEnd}"/>
于 2012-10-05T18:43:09.893 回答
0

你真的需要 xmitdtTo 为空吗?我的意思是,当它为空时,您不会获取或保留重要数据,对吗?如果是这种情况,那么我会通过将 _xmitdtTo 设置为今天作为默认值来规避整个问题。

private DateTime? _xmitdtTo = DateTime.Today; 

如果您需要保留数据,则可以更改属性,改为在 _xmitdtTo 为空时返回今天。

public DateTime? xmitdtTo   
{   
    get
    {
        if (!_xmitdtTo.HasValue)
            return DateTime.Today;
        return this._xmitdtTo; 
    }  

如果你真的想保留你的数据的空性,那么你可以在你的对象上创建一个单独的属性来分配给 xmitdtTo 的 DisplayDateEnd:

public DateTime xmitdtDateEnd
{
    get
    {
        return _xmitdtTo ?? DateTime.Today;
    }
}

不要忘记在 xmitdtTo 属性分配中添加对 notifyPropertyChanged("xmitdtDateEnd") 的调用,以便在您更改 xmitdtTo 时更新 UI。

set 
{ 
    this._xmitdtTo = value; 
    notifyPropertyChanged("xmitdtTo"); 
    notifyPropertyChanged("xmitdtDateEnd"); 
} 
于 2012-10-05T18:58:49.880 回答