当属性更改时如何更改任何控件的外观?
我想向用户突出显示所有已修改的内容,以便他们知道自己更改了什么或在幕后以编程方式更改了哪些内容。
我想使用样式将此逻辑应用到我的所有控件,但我不确定如何操作。我知道我需要创建一个触发器,但不确定到底要触发什么,或者如何获取对绑定属性的任何更改,以便知道它是否已更改。
谢谢。
I'd like to highlight to the user anything that has been modified, so that they know what they have changed or what has been programatically changed behind the scenes for them.
I want to use styles to apply this logic to all my controls, but i'm not sure how. I know I need to create a trigger but not sure what to trigger on exactly or how to pick up any changes to the bound property in order to know if it's changed or not.
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许最好的方法是为您的值编写一个包装类,该包装类实现 INotifyPropertyChanged 并公开一个
object
类型的读/写Value
属性和一个布尔值ValueHasChanged
属性。然后,您可以在Value
setter 中轻松进行更改跟踪:视图模型类不应该公开
string
或DateTime
属性,而应该公开 < code>ValueWrapper 包装其内部字段的属性,例如:然后您可以构建如下样式:
并像这样使用它:
这有一些烦人的事情,例如更改主视图内的属性值模型类现在必须使用
SomeStringProperty.Value = "foo"
而不是SomeStringProperty = "foo"
。Probably the best approach involves writing a wrapper class for your values that implements
INotifyPropertyChanged
and exposes a read/writeValue
property of typeobject
and a booleanValueHasChanged
property. You can then do change tracking pretty easily in theValue
setter:Instead of your view model class exposing
string
orDateTime
properties, it should exposeValueWrapper
properties that wrap its internal fields, e.g.:Then you can build a style like:
and use it like:
There are some annoying things about this, like the fact that to change a property's value inside your main view model class you now have to use
SomeStringProperty.Value = "foo"
instead ofSomeStringProperty = "foo"
.我仍在掌握 WPF 的窍门,因此可能有更好的方法,但这是我认为可行的方法。
创建一个
ValueTracker
类,该类将提供以下 3 个附加依赖属性TrackProperty - 这将是应跟踪的控件的属性
DefaultValue > - 这是被视为默认值的值,其他值将触发样式触发器。
IsDefaultValue - 这将指示当前值是否与默认值匹配,该属性将在触发测试中使用。
这是一个快速测试,它并不完美,但我确信稍加调整就会让事情顺利进行,当然,具有更多 WPF 经验的人可以改进这个想法。
上述可以按如下方式使用。
1- 创建在 ValueTracker.IsDefaultValue 上触发的样式
2- 对于要跟踪的每个控件,附加该样式并设置 ValueTracker.DefaultValue,并使用 ValueTracker.TrackProperty 设置应跟踪的属性。
I am still getting the hang of WPF, so there might be a much better approach, but here is what I thought could work.
Create a
ValueTracker
class, this class will provide the following 3 Attached Dependency PropertiesTrackProperty - This will be the property of the control that should be tracked
DefaultValue - This is the value that is considered the default, and thing else would trigger the Style trigger.
IsDefaultValue - This will indicate if the current value matches the default, this property will be used in the trigger test.
Here is a quick test, it is not perfect, but I am sure a little tweeking will get things going nicely and of course someone with more WPF experience could improve on this idea.
The above can be used as follows.
1- Create a style which triggers on the ValueTracker.IsDefaultValue
2- For each control that you want to track, you attach the style and set the ValueTracker.DefaultValue and set the property that should be tracked using the ValueTracker.TrackProperty.
我建议阅读有关名为 INotifyPropertyChanged 的设计模式的文章,
当您位于类框架上时,您不再位于 UI 线程中,因此您应该使用与 UI 线程相关的调度程序,看看这个
msdn 上的调度程序
假设您想打印新板,您应该这样做像这样,
让我们假设您的代码中有一个按钮,
因此当按下该按钮时,它将更改其内容的可见性。
i would recomed reading about the design pattern called INotifyPropertyChanged
another thing when you are on a class frame you are no longer in the UI thread therefore you should use the dispatcher who is assoicated with the UI thread look at this
dispatcher on msdn
let's say you want to print new board you should be doing it like this
let's assume for a minute you have a button in your code
so when the button is pressed it will change it's content Visibility.