wpf - 将标签宽度绑定到计算的长度属性

发布于 2024-10-02 12:39:46 字数 466 浏览 0 评论 0原文

如果我在视图上有一个标签,我希望其宽度等于同一视图上的一个网格中两列的宽度,那么我将如何在不使用转换器的情况下设置绑定?我应该使用属性来执行计算并存储值吗?我的意图是,如果视图的网格大小发生变化,那么该标签的大小也会发生变化以匹配两列的新宽度。

我应该把这个逻辑放在哪里?我试图遵循 MVVM 模式,但我看到很多关于“MVVM 中的转换器”的线程都说将逻辑放入视图模型中。

我尝试使用视图上的依赖属性来实现此行为,因为我的视图模型在技术上不了解我的视图(那么我的视图模型如何知道我的列当前有多宽?)。但这与我在网上读到的内容相悖。在实现此行为时,我注意到我无法按名称引用我的列,除非我的属性不是静态的,但依赖属性是静态的,因此我不确定如何在不创建更多属性来保存值的情况下调整我的值。

有人可以在这里提供帮助吗?我觉得我把这件事过于复杂化了。我只需要这个标签位于这两列之上,无论它们如何伸展。它只是提供网格中相关字段的可视化分组。一旦我能做到第一个,其他两个应该同样相似。

If I had a label on a view that I wanted to have the width equal to the width of two columns in one of my grids on the same view, how would I set up the binding without using a converter? Should I use properties to preform my calculation and store a value? It is my intention that if the view's grid size changes then this label's size will also change to match the new width of the two columns.

And where should I put this logic? I am trying to follow MVVM pattern but I see that a lot of threads about "converters in MVVM" say to put the logic into the viewmodel.

I tried to implement this behavior with dependency properties on my view since my viewmodel technically has no knowledge of my view (so how would my viewmodel know how wide my columns currently are?). This goes against what I have read online though. When implementing this behavior I noticed that I cannot reference my columns by name unless my property is not static, but dependency properties are static so I am not sure how to shuffle my values around without creating yet more properties to hold values.

Can someone provide help here? I feel like i'm overcomplicating this. I just need this label to sit over these two columns however they stretch. It just provides a visual grouping of related fields in the grid. Once I can do this first one, the other two should be equally similar.

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

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

发布评论

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

评论(1

听不够的曲调 2024-10-09 12:39:46

我的经验法则是,如果它与“View”相关,那么就将其远离 ViewModel。从您的描述来看,这听起来像是纯粹与视图相关,所以我只会在代码隐藏或转换器中使用逻辑。

现在我不明白的是为什么你不愿意使用转换器。使用转换器,您当然不希望存储会导致混乱或重构痛点的业务逻辑,但如果您有一些值需要针对特定​​视图操作进行转换,那么转换器正是您应该使用的。

所以我的建议是转换器...如果它与视图相关,那么请随意使用转换器和代码隐藏...事实上,您应该使用它们而不是 ViewModel。

这有帮助吗?

My rule of thumb is if it's "View" related then keep it away from the ViewModel. From your description this sounds like it's purely view related, so I would just use logic in either the codebehind or a converter.

Now what I don't understand is why you are reluctant to use Converters. With converters you certainly don't want to store business logic that is going to lead to confusion or pain points for refactoring, but if you have some value that needs to be converted for a specific view operation then Converters are exactly what you should be using.

So my advice is Converters ... if it's View related then feel free to use Converters and Codebehind ... in fact you should use them and not the ViewModel.

Does that help?

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