开发者

WPF Binding Today Minus 7 Days

开发者 https://www.devze.com 2023-03-08 11:25 出处:网络
I have a property which is bound to Today\'s date: {开发者_如何学Cx:Static System:DateTime.Today}

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

{开发者_如何学Cx: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.


Expose the value as property in your viewmodel.


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.


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>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消