Binding instance as resource
Asked Answered
P

2

1

I have a very long and repeating binding expression in xaml. Can I first define it in resource section and then refer to it using DynamicResource or StaticResource? Something like:

<SomeControl Visibility="{DynamicResource BindingToParentDataContext}">

The binding expression looks like:

{Binding Path=DataContext.SomeProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:SomeParentUserControl}}, Mode=OneWay, Converter={StaticResource booleanToVisibilityConverter}}
Promise answered 10/2, 2012 at 11:29 Comment(5)
How about answers from similar question?Sherikasherill
Thank you for the useful link. But I am not sure if this will help me. I added to the original question what my binding expression look like.Promise
Like in the mentioned question, you can create custom binding that sets RelativeSource, Mode and Conveter in constructor. It` pretty straightforward.Sherikasherill
Ok, thank you very much. I have another question. The RelativeSource class constructor requires ancestorLevel argument. How do I specify to search for all levels like it happens in xaml?Promise
It looks like AncestorLevel is defaulted to -1, but you can`t set it with value less than 1 yourself. So just use another constructor and set AncestorType directly after creating RelaticeSource.Sherikasherill
R
2

What might work is to write a custom markup extension. This should be a good reference.

But besides that, you can't "predefine" markups like that. Depending on your scenario, you might can put some of these into a datatemplate, which you can reuse.

Reminiscence answered 10/2, 2012 at 13:44 Comment(0)
R
0

You cannot declare a Binding in a resource dictionary, i doubt that this can be made to work using resources.

Righteousness answered 10/2, 2012 at 11:39 Comment(1)
yes, it can work... like this : https://mcmap.net/q/565129/-binding-as-a-resourceCrucial

© 2022 - 2024 — McMap. All rights reserved.