【发布时间】:2012-03-08 07:17:58
【问题描述】:
我在切换到 .NET Framework 4.0 时遇到了一个问题。 我有一个窗口,它在其顶部/左侧和宽度/高度属性上使用双向绑定。
当我需要更改视图模型时出现问题。
更改底层ViewModel后,在我的viewModel对应的propertyname上触发PropertyChanged事件时,触发Left属性的绑定,将窗口移动到正确的X位置。 但是移动窗口的动作会触发“到源”,设置我的 viewModel 的 Top 属性。 EDIT : 没有“设置”完成,但是 Y 绑定没有被处理。
高度和宽度属性的行为相同。
这是一个显示我的问题的小应用程序。
这是模型:
public class Model : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public Position SelectedPos { get; set; }
public Position Pos1 { get; set; }
public Position Pos2 { get; set; }
public Model( int x, int y, int x2, int y2 )
{
Pos1 = new Position( x, y );
Pos2 = new Position( x2, y2 );
SelectedPos = Pos1;
}
public void Toggle()
{
SelectedPos = Pos2;
if( PropertyChanged != null )
{
var e = new PropertyChangedEventArgs( "SelectedPos" );
PropertyChanged( this, e );
}
}
}
public class Position
{
int _x;
public int X
{
get { return _x; }
set { _x = value; }
}
int _y;
public int Y
{
get { return _y; }
set { _y = value; }
}
public Position( int x, int y )
{
X = x;
Y = y;
}
}
这是视图:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
Left="{Binding Mode=TwoWay, Path=SelectedPos.X}"
Top="{Binding Mode=TwoWay, Path=SelectedPos.Y}">
<Grid>
<Button Click="Button_Click">Change Position</Button>
</Grid>
</Window>
最后是代码隐藏:
namespace WpfApplication1
{
public partial class MainWindow : Window
{
Model model;
public MainWindow()
{
InitializeComponent();
model = new Model( 5, 5, 500, 500 );
DataContext = model;
}
private void Button_Click( object sender, RoutedEventArgs e )
{
model.Toggle();
}
}
}
我想知道的是,是否有某种方法可以“冻结”绑定,以防止引擎设置我的 viewModel,直到它处理完我要求它执行的所有绑定。或者在短时间内将绑定从 twoWay 切换到 OneWay。
这里的小应用程序在使用 .NET framework 3.5 时行为正确,但没有 4.0。
我很惊讶我找不到任何人在同样的问题上挣扎,我做错了什么吗? 感谢您的回答,如果有不清楚的地方,请随时询问。
让-卢普·卡伦
我今天早上已经添加了日志(我应该在发布之前就这样做了..),就像你做的那样,你是对的,没有完成“设置”,但是 Y 绑定没有被处理。 当实际切换几次窗口位置时,这种行为更加奇怪。
我将测试您的解决方案,即使我希望避免使用后面的代码(我使用一些代码仅用于测试目的)。
感谢您如此迅速地回答,当我有时间找到无代码隐藏的解决方案时,我会回复您。 由于未设置模型,我可以在触发行为怪异的“位置”绑定之后立即使用 propertychanged 事件分别触发 X 和 Y 绑定。
再次感谢,我走错路了,你为我节省了大量时间。
【问题讨论】:
标签: wpf binding height width two-way-binding