WPF Bind item to parent window

I am trying to associate a property of an element in a child control with a property element in the parent window, this does not work.

Here is what I am trying to do: enter image description here

Here is the xaml that does not work:

CurrentDate="{Binding ElementName=TimeBar, Path=SelectionStart,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" 

Thanks.

+4
source share
3 answers

create a dependency property in your usercontrol and then bind to it in your window

something like this: DependencyProperty implementations you can find here in stackoverflow

<YourUsercontrol x:Name="uc">
  <YourSomeControl CurrentDate="{Binding ElementName=uc, Path=MyDp}"/>
 </YourUsercontrol>

xaml window

 <Window>
   <ElementInParent x:Name="eip" />
   <YourUsercontrol MyDp="{Binding ElementName=eip, Path=PropertyFromElementInParent}"/>
+2
source

based on the following LINK answer , by default SelectionStart is not a Bindable Probperty, so you need to create an attached behavior or something like that

0

ElementName Relative Source - . , UserControl ElementName , XAML.

- , , .

: . .

Text DataContext ( ).

<Window
     xmlns:self="clr-namespace:experiments"
     >
    <StackPanel>
        <TextBox x:Name="Name" Width="100"/>
        <self:UserControl1 DataContext="{Binding ElementName=Name}"/>
    </StackPanel>
</Window>



<UserControl x:Class="experiments.UserControl1">
    <Grid>
        <TextBlock Text="{Binding Path=Text}" Width="100" Background="AliceBlue" Height="50"/>
    </Grid>
</UserControl>
0

All Articles