[英]Window property databinding not updating when INotifyPropertyChanged sent, but child element properties are
I have a window whose properties and child element properties bind to a class called Data
: 我有一个窗口,其属性和子元素属性绑定到名为Data
的类:
public TerminalOverlay(Data dataContext)
{
DataInstance = dataContext;
DataContext = DataInstance;
InitializeComponent();
}
The window TerminalOverlay
is created in my MainWindow
window, as follows: 在我的MainWindow
窗口中创建了TerminalOverlay
窗口,如下所示:
public void MainWindow_Loaded(object sender, EventArgs e)
{
_terminalOverlayWindow = new TerminalOverlay(_dataInstance);
_terminalOverlayWindow.Owner = this;
_terminalOverlayWindow.Show();
}
_dataInstance
is instantiated in the constructor of MainWindow, and one of the "problem" properties in it is the following: _dataInstance
在MainWindow的构造函数中实例化,并且其中的“问题”属性之一如下:
public double ? PosX
{
get
{
return _posX == null ? _defaultPosX : _posX;
}
set
{
_posX = value;
OnPropertyChanged("PosX");
}
}
Where OnPropertyChanged is implemented as follows: public event PropertyChangedEventHandler PropertyChanged; 其中,OnPropertyChanged的实现如下:公共事件PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 公共无效OnPropertyChanged(字符串propertyName){如果(PropertyChanged!= null){PropertyChanged(this,new PropertyChangedEventArgs(propertyName)); } } }}
The Data
object is passed from the main window which creates TerminalOverlay
. Data
对象从创建TerminalOverlay
的主窗口传递。 The Data object also implements INotifyPropertyChanged
, so when I update properties in the Data object from the main window, they are reflected in the TerminalOverlay
window. Data对象还实现INotifyPropertyChanged
,因此当我从主窗口更新Data对象中的属性时,它们会反映在TerminalOverlay
窗口中。
However, this is only the case for the child elements of the TerminalOverlay
window. 但是,这仅适用于TerminalOverlay
窗口的子元素。 The properties of the TerminalOverlay
window itself are initially set to the values stored in the Data class, but they do not seem to update, even though the child elements do. TerminalOverlay
窗口本身的属性最初设置为存储在Data类中的值,但是即使子元素确实存在,它们似乎也没有更新。
What am I doing wrong? 我究竟做错了什么? Looking in the visual tree I found that TerminalOverlay.DataContext.TopX
did update, it's just that the window isn't being notified to update. 在可视化树中查看时,我发现TerminalOverlay.DataContext.TopX
确实已更新,只是没有通知窗口更新。
Also, TerminalOverlay.xaml looks like the following: 另外,TerminalOverlay.xaml如下所示:
<Window x:Class="Background_Terminal.TerminalOverlay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Background_Terminal"
mc:Ignorable="d"
Title="TerminalOverlay" Height="200" Width="800" Left="{Binding PosX, Mode=OneWay}" Top="{Binding PosY, Mode=OneWay}" AllowsTransparency="True" WindowStyle="None" ResizeMode="NoResize" Background="Transparent" Loaded="TerminalOverlay_Loaded">
<Grid>
<TextBox x:Name="TerminalData_TextBox" BorderThickness="0" FontFamily="Consolas" Background="Transparent" IsReadOnly="True" IsReadOnlyCaretVisible="True" FontSize="{Binding FontSize}" Foreground="{Binding FontColor}" Text="{Binding TerminalDataDisplay, Mode=OneWay}"/>
<TextBox x:Name="Input_TextBox" VerticalAlignment="Bottom" FontSize="{Binding FontSize}" Foreground="{Binding FontColor}" />
</Grid>
</Window>
The properties like FontSize
in Input_TextBox
update properly, but Top
and Left
in the Window properties do not. 诸如Input_TextBox
FontSize
类的属性会正确更新,但Window属性中的Top
和Left
不会正确更新。
your posX is a Nullable variable : they have "special" binding art 您的posX是一个Nullable变量:它们具有“特殊”绑定图
try this here 在这里试试
Left="{Binding PosX, Mode=TwoWay, TargetNullValue=''}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.