I\'ve looked long and hard and am stuck. I\'m trying to pass a parameter from Window to UserControl1 via a binding from Window.
In the MainWindow, the UserControl1 is i
In the window XAML, the bindings on the usercontrol instance use the usercontrol's DataContext as their source, by default. You're assuming that it's inheriting its datacontext from the window.
But here's this in the UserControl:
DataContext="{Binding RelativeSource={RelativeSource Self}}"
That breaks all the bindings the parent gives it. So don't do that. Use relativesource:
<UserControl x:Class="MyParamaterizedTest3.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MyParamaterizedTest3"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
>
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Border BorderThickness="3" BorderBrush="Black">
<StackPanel>
<TextBlock Text="{Binding MyCustom, RelativeSource={RelativeSource AncestorType=UserControl}, FallbackValue=mycustom}"></TextBlock>
</StackPanel>
</Border>
</Grid>
</UserControl>
Also:
UpdateSourceTrigger=PropertyChanged
doesn't serve any purpose on a binding to a property that never updates its source, so that can be omitted.
As we discussed in comments, INotifyPropertyChanged
isn't needed for dependency properties.
It's immensely frustrating when bindings just don't work, because how do you debug them? You can't see anything. The critical thing is where is it looking for this property? You can get diagnostic information like this:
<TextBlock
Text="{Binding MyCustom, PresentationTraceSources.TraceLevel=High, FallbackValue=mycustom}"></TextBlock>
That will emit a great deal of debugging information to the Output pane of Visual Studio at runtime. It will tell you exactly what the Binding is trying to do, step by step, what it finds, and where it fails.
The window can get away with setting its own DataContext to Self because it has no parent, so it's not stepping on an inherited DataContext. However, the window can and should use RelativeSource itself -- or better yet, write a main viewmodel class (you know how to implement INPC already), move the window's properties to the main viewmodel, and assign an instance of the viewmodel to the window's DataContext.