【发布时间】:2015-06-25 10:55:22
【问题描述】:
我在更新绑定时遇到问题。 但我认为解释我的问题的最简单方法是我的代码:
XAML
<StackPanel>
<StackPanel.Resources>
<Converter:Converter_Position x:Key="Position"/>
</StackPanel.Resources>
<TextBox Text="{Binding Path=Position.X, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Text="{Binding Path=Position, Converter={StaticResource PositionToStartPosition}, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
如果我更改第一个 TextBox 的文本,第二个 TextBox 不会更新。
我的转换器:
class Converter_Position : IValueConverter
{
public object Convert(object value, Type t, object parameter, CultureInfo culture)
{
RaPoint Position = value as RaPoint;
return Position.ToString();
}
public object ConvertBack(object value, Type t, object parameter, CultureInfo culture)
{
throw new Exception();
}
}
债券类别:
public class RaPoint : INodifyPropertyChanged
{
public RaPoint()
{
X = 0;
Y = 0;
}
public RaPoint(double X, double Y)
{
this.X = X;
this.Y = Y;
}
private const string XPropertyName = "X";
private double _X;
public double X
{
get
{
return _X;
}
set
{
_X = value;
RaisePropertyChanged(XPropertyName);
}
}
private const string YPropertyName = "Y";
private double _Y;
public double Y
{
get
{
return _Y;
}
set
{
_Y = value;
RaisePropertyChanged(YPropertyName);
}
}
public override string ToString()
{
return String.Format("X:{0} Y:{1}" , X.ToString(), Y.ToString());
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
数据上下文:
private const string PositionPropertyName = "Position";
private RaPoint _Position = new RaPoint();
public RaPoint Position
{
get
{
return _Position;
}
set
{
_Position = value;
RaisePropertyChanged(PositionPropertyName);
}
}
【问题讨论】:
标签: c# wpf binding inotifypropertychanged