asusralis
Topic Author
Posts: 121
Joined: 30 Jul 2018, 05:03

ElementName does not work with custom dependency property

17 Nov 2021, 17:38

public static readonly DependencyProperty ValidStatsProperty = DependencyProperty.Register("ValidStats", typeof(object), typeof(StatView),
            new PropertyMetadata(OnValidStatsChanged));  
<local:StatView Stats="{Binding Colonist.Stats}" ValidStats="{Binding ElementName=root, Path=DataContext.StatType}" />
<TextBlock Text="{Binding ElementName=root, Path=DataContext.StatType}" />
This is inside a ItemsControl's ItemTemplate. The TextBlock's Text binds correctly to the StatType, but ValidStats does not. OnValidStatsChanged is never called. However, if I use a StatView outside of the ItemTemplate:
                    <local:StatView
                        Stats="{Binding TotalStats}"
                        ValidStats="{Binding StatType}" />
ValidStats is set and OnValidStatsChanged is called. Why does this property not work when using ElementName?
 
User avatar
sfernandez
Site Admin
Posts: 2417
Joined: 22 Dec 2011, 19:20

Re: ElementName does not work with custom depedency property.

17 Nov 2021, 18:53

Is StatView a control or what class is inheriting from?
 
asusralis
Topic Author
Posts: 121
Joined: 30 Jul 2018, 05:03

Re: ElementName does not work with custom depedency property.

18 Nov 2021, 02:10

StatView is a UserControl.
 
User avatar
sfernandez
Site Admin
Posts: 2417
Joined: 22 Dec 2011, 19:20

Re: ElementName does not work with custom depedency property.

22 Nov 2021, 13:36

I was able to reproduce it if I use the same x:Name for the UserControl and for the control containing the template. Could you please report this problem in our bugtracker?
As a workaround for now you can use a different name in the template:
<Grid x:Name="root2" ...>
  ...
  <DataTemplate>
    <Grid>
      <local:StatView Stats="{Binding Colonist.Stats}" ValidStats="{Binding ElementName=root2, Path=DataContext.StatType}" />
   </Grid>
 </DataTemplate>
 ...
 </Grid>
Does that work for you?

Who is online

Users browsing this forum: Google [Bot], lehoang03an and 3 guests