David Ward
David Ward

Reputation: 3829

WPF Binding Today Minus 7 Days

I have a property which is bound to Today's date:

{x:Static System:DateTime.Today}

How can I extend the binding to Today.AddDays(-7)? I know that I could use a converter but I wanted to avoid the extra code if it is possible.

Upvotes: 2

Views: 349

Answers (4)

biju
biju

Reputation: 18030

It can't be done using XAML only.Either you have to use a value converter or you have to bind to a View model property which returns the specified value

Upvotes: 0

Emond
Emond

Reputation: 50692

Have a look at the ObjectDataProvider. It will allow you to bind to a method.

From this post

<ObjectDataProvider x:Key="ADUsers"
                    ObjectType="{x:Type src:PDSAADUsers}"
                    MethodName="GetUsers">
    <ObjectDataProvider.MethodParameters>
        <x:Static Member="system:String.Empty" />
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

Upvotes: 0

Cameron MacFarland
Cameron MacFarland

Reputation: 71936

Yeah this isn't possible in pure XAML.

You'll need either a converter (a generic date manipulation converter, or a specific one), or if you're using MVVM or other UI design pattern creating a property in your bound object to hold the value you want.

Upvotes: 1

Oliver Weichhold
Oliver Weichhold

Reputation: 10306

Expose the value as property in your viewmodel.

Upvotes: 2

Related Questions