0

I'm having trouble passing a value from one user control to a dependency property on a child user control. I think my issue might lie somewhere within the data context, but I've spent quite a bit of time searching stackoverflow and trying different tweaks to the bindings and data context and can't get this working. The snippet below shows what works and what doesn't. I can successfully bind the parent property to a label, and I can successfully pass a hard-coded value to the child user control, but I cannot pass the parent property binding to the user control.

<UserControl x:Class="MyParentUC.MyParentUC_View"
             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:MyParentUC"
             xmlns:MyChildUC="clr-namespace:MyChildUC;assembly=MyChildUC"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">

    <StackPanel Orientation="Vertical">
        <Label Content="{Binding MyInteger}" /> <!-- works -->
        <Grid>
            <MyChildUC:ChildUC VerticalAlignment="Top" MyInteger="{Binding Path=MyInteger}" /> <!-- doesn't work -->
        </Grid>
        <Grid>
            <MyChildUC:ChildUC VerticalAlignment="Top" MyInteger="123" /> <!-- works -->
        </Grid>
    </StackPanel>
</UserControl>
    
  • 1
    A common mistake would be that ChildUC sets its own DataContext. Which data binding error message do you observe in the Output Window in Visual Studio when you debug the program? – Clemens Jan 29 '21 at 17:14
  • ChidUC was setting its own data context. I removed that, and set the data context in a StackPanel within ChildUC, and now it seems to work. Thank you! :) – Craig Stewart Jan 29 '21 at 20:04

0 Answers0