如何编写伪只写依赖属性?

发布于 2024-10-25 02:48:19 字数 1165 浏览 5 评论 0原文

好吧,这只是我脑海中浮现的关于某种“伪写”依赖属性的想法。我说有点是因为我实际上什至不希望存储该值,而是希望将传递给设置器的值设置其他属性。作为一个假设的例子,我想这样做...

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

而不是这个...

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

只是一个 XAML 键入方便的东西,它让我可以通过更容易键入的东西来设置实际的四个属性,这要归功于只写附加属性。沿着折叠的 CSS 规则的方式思考。实际上,为了这个效果,这里是另一个...

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

而不是...

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

现在以第一个为例,一个想法是在内部存储这样的结构,然后将其与现有的 Width、Height、Canvas.Left 和 Canvas 同步。顶级属性,但我真的不打算将其读回,因此不需要。 (从技术上讲,我可以完全忽略它的存储,只在 propertychanged 处理程序中调用 ClearValue,但同样,它并不是真正用于将其读回,因此这只是为了一致性。)

从技术上讲,我什至不希望/不需要这是一个 DP但我的理解是,如果您需要从 XAML 设置它,它必须是 DP,这就是我所追求的。如果我们可以设置一个直接的 .NET 属性,那就完美了,因为我可以设置实现,并根据需要重建自身,但同样,我知道您无法从 XAML 访问 .NET 属性,因此我'我马上回来了。

现在,是的,我知道这个概念会受到很多人的反对,尤其是 XAML 纯粹主义者,但我问的是如何而不是为什么,所以如果没有其他原因,将此视为“可以完成”而不是“应该完成”的练习。我什至不建议我使用这个。我只是想更好地理解 DP 和 XAML 的局限性,并且我发现这样的练习非常有帮助。

那么,想法??

中号

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.Left 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芸娘子的小脾气 2024-11-01 02:48:19

在不编写自己的 XAML 编译器的极端情况下,我认为您能做的最好的事情就是使用附加属性,根据您定义的一些紧凑语法来设置您感兴趣的属性组。是的,这意味着您已经为紧凑表示分配了存储空间,但正如您所说,您可以在应用后立即清除它。这有点不正统,但在技术上是可行的。

您还可以尝试根本不声明附加属性字段,以便通过 get/set 方法访问该值(尚未尝试过此操作,但我相信当 DP 是私有的时它可以工作,所以也许这会工作)。这样,您实际上也可以使其可读,而无需费心侦听相关属性的更改。当然,这意味着您的属性不会有更改通知:

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

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

我只是将紧凑表示形式放入上面的字符串中,但您可能希望使用特定类型。同样,上述内容可能不起作用 - 您可能需要关联的 DependencyProperty 实例。如果我在 Windows 机器上,我会尝试一下。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文