Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicitly set WPF binding datatype

I wanted to ask if WPF has any functionality by which one can define a target type on a binding when the binding is done to an object of type "object". I have a generic collection of type "object" that must be handled as one object type or the other (ie. DateTime, int, etc.) at its various binding points.

Is there any way that I can arbitrarily force the .Net framework to treat objects of unknown type at compile time as objects of Type_X, on a case-by-case basis when handling bindings?

like image 347
tris Avatar asked May 11 '26 07:05

tris


1 Answers

Sure, you can use DataTemplates. If you define them in your App.xaml, they will be applied globally:

<DataTemplate DataType="{x:Type local:YourType}">
    <TextBox Text="{Binding SomeProperty}" HorizontalAlignment="Stretch"/>
</DataTemplate>

Now whenever you add an object of runtime type YourType somewhere in your UI, like in a StackPanel, that Template will be used.

like image 88
Botz3000 Avatar answered May 12 '26 22:05

Botz3000



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!