我有一个滑块,它的值属性绑定到依赖项属性。我需要知道用户是否通过GUI更改了值。不幸的是,此滑块的值通常通过代码更改,并且“Value_Changed”事件会在发生时触发。
我知道有两种解决方法:
我只是想知道是否有更好的方法来了解用户是否通过UI更改了值?
答案 0 :(得分:2)
我这样做:
public bool PositionModifiedByUser
{ /* implement IPropertyChanged if need to bind to this property */ }
// use this property from code
public double Position
{
get { return m_position ; }
set { SetPropertyValue ("PositionUI", ref m_position, value) ;
PositionModifiedByUser = false ; }
}
// bind to this property from the UI
public double PositionUI
{
get { return m_position ; }
set { if (SetPropertyValue ("PositionUI", ref m_position, value))
PositionModifiedByUser = true ; }
}
SetPropertyValue是一个帮助程序,它检查相等性并在值实际发生更改时触发属性更改通知。
答案 1 :(得分:0)
答案 2 :(得分:0)
但安东的回答是更好的+1
[BindableAttribute(true)]
public double Slider1Value
{
get { return slider1Value; }
set
{
// only bind to the UI so any call to here came from the UI
if (slider1Value == value) return;
// do what you were going to do in value changed here
slider1Value = value;
}
}
private void clickHalf(object sender, RoutedEventArgs e)
{
// manipulate the private varible so set is not called
slider1Value = slider1Value / 2;
NotifyPropertyChanged("Slider1Value");
}