WPF - 绑定的标签宽度来计算length属性 [英] wpf - bind label widths to calculated length property

查看:546
本文介绍了WPF - 绑定的标签宽度来计算length属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有,我想有宽度等于我上了同样的看法网格的一个两列的宽度的观点,我将如何设置,而无需使用转换器的绑定标签吗?我应该使用属性preform我的计算和存储的价值?这是我的意图,如果视图的网格大小的变化那么这个标签的大小也将改变以匹配两列的新宽度。

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.

和我应该在哪里把这个逻辑是什么?我试图遵循MVVM模式,但我看到了很多关于在MVVM转换器线程说把逻辑到视图模型。

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.

推荐答案

我的经验法则是,如果它的查看,然后把它从视图模型远有关。从你的描述,这听起来像它的纯粹查看相关的,所以我只想用逻辑无论是在codebehind或转换器。

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.

所以我的建议是转换器......如果它查看相关然后随意使用转换器和codebehind ......其实你的使用它们,而不是视图模型。

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.

帮助吗?

这篇关于WPF - 绑定的标签宽度来计算length属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆