1

All I want is when a user changes the value in the textbox alphaMin_txt, the property AlphaMin gets updated.

Code behind:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private string _alphaMin;

    public string AlphaMin
    {
        get { return _alphaMin; }
        set
        {
            if (value != _alphaMin)
            {
                _alphaMin = value;
                NotifyPropertyChanged();
            }
        }
    }
}

XAML:

<DockPanel DataContext="{Binding MainWindow}">
    <TextBox Text="{Binding 
                      Path=AlphaMin, 
                      NotifyOnTargetUpdated=True,
                      Mode=OneWayToSource,    
                      UpdateSourceTrigger=PropertyChanged}" />
 </DockPanel>

This should be a duplicate a hundred times over but I've been through it all and none of it is laid out plain and simple for this one-way update of the source. All the MSN tutorials are binding some UIControl to another, which is pointless because IntelliSense shows you how to do that.

AndrewBenjamin
  • 651
  • 1
  • 7
  • 16

2 Answers2

3

Your DockPanel probably has a faulty DataContext binding. DataContext should be set at the window level.

<Window ... DataContext="{Binding RelativeSource={RelativeSource Self}}" ..>

Of course, this is assuming your XAML is MainWindow.xaml.

If you have a different DataContext for the rest of the MainWindow, then you can do this:

<TextBox Text="{Binding
         RelativeSource={RelativeSource AncestorType=Window},
         Path=AlphaMin,
         NotifyOnTargetUpdated=True,
         Mode=OneWayToSource,
         UpdateSourceTrigger=PropertyChanged}" />

Of course, you should remove the DataContext for the DockPanel.

Your code behind is correct; there are no changes needed. Using CallerMemberName is a good way to implement INotifyPropertyChanged.

Jai
  • 8,165
  • 2
  • 21
  • 52
  • This worked. Thank you! I ended up setting it like `` where `Constants` is the class I used instead of MainWindow. I'm just stoked it worked. – AndrewBenjamin Jun 27 '16 at 05:45
  • @AndrewBenjamin If your DataContext is not the window itself, then the problem should be the DockPanel's DataContext is written wrongly. That means it has nothing to do with the fact that you set the DataContext at the wrong place. `` would probably have worked too. Just stating, in case you got the wrong understanding of the problem, which may cause you to make more mistakes in the future. – Jai Jun 27 '16 at 05:54
  • along the same lines ... is there a setting to cause `AlphaMin` to update when `myTextbox.Text` is set programmatically? Or should I switch to TwoWay and only update `AlphaMin` programmatically? – AndrewBenjamin Jun 27 '16 at 06:21
  • @AndrewBenjamin Isn't that what the binding is supposed to do? – Jai Jun 27 '16 at 06:26
  • When I do `alphaMin_txt.Text = "1.1"`, `AlphaMin` is never updated until I start typing in the textbox again. Would be nice if the binding just took care of this. Perhaps its one of my settings. – AndrewBenjamin Jun 27 '16 at 06:33
1
  1. Assign a name to <Window x:Name="MyWin"...> , then change DataContext binding to {Binding ElementName=MyWin}.

  2. Change this NotifyPropertyChanged(); to NotifyPropertyChanged("AlphaMin");

AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38
  • I think this also would work. However I don't have to specify `"AlphaMin"` because I have defaulted to `[CallerMemberName]`. – AndrewBenjamin Jun 27 '16 at 06:24