如何将 winforms 不透明度绑定到 TrackBar(滑块)
我有一个带有 BindingSource
的 winform,它的 DataSource
中有一个名为 Opacity
的 int 属性。我在 winform 上还有一个 TrackBar
,我想用它来控制 winform 的 Opacity
。
我已将 TrackBar
上的 Value
属性绑定到 Opacity
,并且可以很好地滑动 TrackBar
会将变量从 TrackBar.Minimum
(0) 更改为 TrackBar.Maximum
(1)。
不过,我还将 winform 的 Opacity 属性绑定到该值,因为 TrackBar 的值仅在 +/-1 范围内从最小值到最大值,而不是+/- .1 左右(就像 Opacity
那样),它不能正确淡入 winform。相反,0 将使其不透明,1 将使其完全可见。
我需要一种在上述架构中工作的方法,但让 TrackBar
以小于 1 的定义增量将其值从 0 更改为 1。
I've got a winform with a BindingSource
that has an int property named Opacity
in its DataSource
. I also have a TrackBar
on the winform that I want to use to control the Opacity
of the winform.
I've bound the Value
property on the TrackBar
to the Opacity
and that functions just fine, sliding the TrackBar
will change the variable from TrackBar.Minimum
(0) to TrackBar.Maximum
(1).
I've also bound the Opacity
property of the winform to this value, however, since the TrackBar
's values only go from Minimum to Maximum in +/-1 rather than +/- .1 or so (like Opacity
does), it doesn't properly fade the winform. Instead, 0 will turn it opaque and 1 will turn it fully visible.
I need a way to work within the architecture described above, but get the TrackBar
to change its value from 0 to 1 in defined increments smaller than 1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为最简洁的方法是创建一个直接继承自
TrackBar
的UserControl
,它隐藏Value
、SmallChange
、Minimum
、Maximum
和TickFrequency
属性以及同名的double
属性:I think the cleanest way to do this would be to create a
UserControl
that inherits directly fromTrackBar
, that hides theValue
,SmallChange
,Minimum
,Maximum
andTickFrequency
properties with same-nameddouble
properties:如果您进行数据绑定,那么您有一个数据类,该数据类具有一个
double OpacityForSlider
(或类似的东西),您已绑定了TrackBar
的Value
代码> 到.将滑块最小值和最大值设置为 0-100 并将其添加到您的数据类中
现在将您的 winforms
Opacity
绑定到它,而不是OpacityForSlider
If you're databinding then you have a data class that has a
double OpacityForSlider
(or something similar) that you've bound theValue
of theTrackBar
to.Set the sliders min and max to 0-100 and add this to your data class
Now bind your winforms
Opacity
to that, rather thanOpacityForSlider