开发者

How do I write a pseudo-write-only dependency property?

Ok, so this is just an idea I have floating in my head regarding a sort of 'pseudo-write--only' dependency property. I say sort of because I actually don't even want the value to be stored, but rather I want the value I pass to the setter to set other properties. As a hypothetical example, I'd like to do this...

<button UIHelper.Bounds="10,10,40,20" />

instead of this...

<button Canvas.Left="10" Canvas.Top="10" Width="40" Height="20" />

Just a XAML-typing convenience thing that lets me set the actual four properties with something easier to type thanks to say a write-only attached property. Think along the way of collapsed CSS rules. Actually, to that effect, here's another...

<border UIHelper.BrushSpec="AliceBlue Red 2 4" />

instead of...

<border Background="AliceBlue" BorderBrush="Red" BorderThickness="2" CornerRadius="4" />

Now using the first as an example, one idea is to store such a structure internally, then sync it with the existing Width, Height, Canvas.开发者_如何学GoLeft and Canvas.Top properties, but I really don't ever plan on reading it back out so that isn't needed. (Technically I could ignore the storing of it altogether and just call ClearValue in the propertychanged handler but again, it's not really for reading it back out so that's just for consistency.)

Technically I don't even want/need this to be a DP except my understanding is if you need to set it from XAML, it has to be a DP, hence what I'm after. It would be perfect if we could just set a straight-up .NET property as I could make the implementation both set, and reconstruct itself as needed, but again, I understand you can't access .NET properties from XAML, hence I'm right back here.

Now yes I know this concept will be frowned upon by a lot of people, especially the XAML purists, but I'm asking how not why so if for no other reason, consider this an exercise in 'can it be done' not 'should it be done'. I'm not even suggesting this is what I'll use. I'm just trying to better understand the limits of DPs and XAML and I find exercises like this to be really helpful.

So, thoughts??

M


Without going to the extreme of writing your own XAML compiler, I think the best you can do is an attached property that sets the group of properties you're interested in based on some compact syntax you define. Yes, this means you've allocated storage for the compact representation, but as you say you could immediately clear it after applying. It would be a little unorthodox, but it would technically work.

You could also try not declaring an attached property field at all so that the value is accessed via the get/set methods (haven't tried this, but I believe it works when the DP is private, so perhaps this will work). This way, you could actually have it readable too without going to the trouble of listening for changes to relevant properties. Of course, it means your property won't have change notification:

public static string GetBounds(UIElement uiElement)
{
    return Canvas.GetLeft(uiElement) + "," + ...;
}

public static void SetBounds(UIElement uiElement, string compact)
{
    // parse and apply compact
}

I'm just putting the compact representation in a string above, but you'd probably want to use a specific type. Again, the above may not work - you may need the associated DependencyProperty instance. If I was on a Windows machine, I'd try it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜