question

Ianturner-3100 avatar image
0 Votes"
Ianturner-3100 asked ZhanglongWu-MSFT commented

Textblock for current selected index will not update from the background data class

I am using the NorthWind Db to learn WPF, and have a window with several datagrids on it showing the data in various tables from the Db. I have Textblocks above the datagrids showing total records and current selected record.

I have defined them as shown below in my XAML

     <TextBlock x:Name="CustCurrent"                     
             Text="{Binding CustomerCurrent, UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource FindAncestor, AncestorType=local:nwcustomer}}"
              Height="25"
              FontWeight="Bold"
              Padding="3"
              VerticalAlignment="Center"
                  Background="Cyan"
                  Width="390"
                     
              Foreground="{StaticResource Black1}"/>

the property Customercurrent is a full property defined in my nwcustomer class as shown here :
public int CustomerCurrent
{
get
{
return customerCurrent;
}
set
{
customerCurrent = value;
OnPropertyChanged ( nameof ( CustomerCurrent ) );
}
}

when debugging, the Set is called as expected, but the Get is never called to fill my TextBlock ??

Any help would be gatefully received ?

windows-wpf
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


When is Set called?

0 Votes 0 ·

It is set in code behind on the SelectionChanged event, which does then trigger the Set in the debugger.

Equally, If I use the same value I just set from code behind to fill out the Text field, it calls the Get and gives me the correct value. It is just that the XAML code dos NOT call the Get.

I notice in Binding window that it shows the DataContext to be null, but I set that also in Code behind when window opens.

0 Votes 0 ·

If you assign an nwcustomer object to DataContext, then try “Text={Binding CustomerCurrent}” without RelativeSource.



0 Votes 0 ·

1 Answer

Ianturner-3100 avatar image
0 Votes"
Ianturner-3100 answered ZhanglongWu-MSFT commented

I have been trying to do this, but without any success :
my xaml has the following setup for this issue

   xmlns:local="clr-namespace:WPFPages.Views"

 <Window.Resources>
        
     <local:nwcustomer  x:Key="CustDb"/>

     <TextBlock x:Name="CustomersTot"
             DataContext="{Binding CustDb}"
             Text="{Binding CustomersTotal , UpdateSourceTrigger=PropertyChanged}"
              FontWeight="Bold"
              Height="25"
              Padding="3"
              VerticalAlignment="Center"
              Foreground="{StaticResource Black1}"/>

hovering over Binding CustDb tells me "no Datacontext found for Binding "CustDb"" but hovering over the "Binding CustomerTotal" tells me it IS a PropertyPath ????
Equally, hovering over =PropertyChanged says "No DataContext for Binding PropertyChanged

And of course, it does not work. I am a total loss here ??

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

More info : Binding failures shows the following when the window loads

NB The name is wrong above, the property is actually "CustomerCurrent", Not CustomerTotal, which is another property in the same source class

118702-image.png



I also notice that debug lines in the CustomerCurrent property itself are NOT call at any time, even during
the window loading, which it used to do ?

Sorry for any confusion, but the xaml code is now :

         <TextBlock x:Name="CustCurrent"    
             DataContext="{Binding DbCust}"
             Text="{Binding CustomerCurrent, diag:PresentationTraceSources.TraceLevel=High}"
              Height="25"
              FontWeight="Bold"
              Padding="3"
              VerticalAlignment="Center"
              Background="Transparent"
              Width="30"                     
              Foreground="{StaticResource Black1}"/>

Hovering over CustomerCurrent in the Binding phrase gives me Int32, which shows it must be finding the property, which is indeed an Int32 value.

0 Votes 0 ·
image.png (50.0 KiB)

Hi @Ianturner-3100,

Could you please share a complete sample to reproduce the issue.

Best regards,
Zhanglong

0 Votes 0 ·