简体   繁体   中英

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 :

public TerminalOverlay(Data dataContext)
{
    DataInstance = dataContext;
    DataContext = DataInstance;

    InitializeComponent();
}

The window TerminalOverlay is created in my MainWindow window, as follows:

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:

public double ? PosX
{
    get
    {
        return _posX == null ? _defaultPosX : _posX;
    }
    set
    {
        _posX = value;

        OnPropertyChanged("PosX");
    }
}

Where OnPropertyChanged is implemented as follows: public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }

The Data object is passed from the main window which creates 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.

However, this is only the case for the child elements of the TerminalOverlay window. 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.

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.

Also, TerminalOverlay.xaml looks like the following:

<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.

your posX is a Nullable variable : they have "special" binding art

try this here

Left="{Binding PosX, Mode=TwoWay, TargetNullValue=''}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM