0

Code first (upper TextBox is simplified for purpose of this question) :

<TextBlock
                Style="{StaticResource  FieldNameStyle }"
                TextWrapping ="Wrap" Height="33" FontSize="12"
                Visibility="Visible"
                TextAlignment="Center"
                Foreground="#FFFFFF"
                Opacity="0.5"                                      
                Text="{Binding UnderLineMsg}">      

                <Hyperlink  Name="PrivacyNoticeLink2"                                 
                Command="{Binding OpenPrivacyNoticeCommand}">   
                    <TextBlock 
                        Visibility="Visible"
                        Name="privacyNoticeText2"
                        Text="{Binding PrivacyNoticeButtonLabel,FallbackValue='privacy notice' ,UpdateSourceTrigger=PropertyChanged}"/>                        
                </Hyperlink> 
            </TextBlock>

this is what it looks after the window loads for the first time : Under line msg filler: link

one of the events in the window triggers a call to

OnPropertyChanged(null);

the method triggers a "refresh" in all the members in the window that are subscribed to it with :

 UpdateSourceTrigger=PropertyChanged

once called the Hyperlink element disappears completely (verified using Snoop 2.8) so after the call it will look like this: Under line msg filler:

i have NO idea why this is happening. the current fix is replacing the general OnPropertyChanged call with many specific ones but that is not a realistic option in the long run.

EDIT : Isolated the issue to a new project, note the issue still happens when its only a textblock within a textblock

simple XAML with a button that triggers OnPropertyChanged

<Grid>
        <Button Click="Meh"   Margin="171,37,153,199">
            PRESS ME
        </Button>

        <TextBlock  Name="WrapperText" Text= "{Binding randomNumber}">

                <TextBlock  Name="linkText" Text="{Binding randomNumStr }"></TextBlock>

        </TextBlock>
</Grid>

Code behind:

public MainWindow()
    {
        DataContext = new Stuff();
        InitializeComponent();

    }

    public void Meh(object sender, RoutedEventArgs e)
    {
        //MessageBox.Show(this, "BLA", "caption", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
        //MessageBox.Show("FASDFASDFASDF");
       (DataContext as Stuff).OnPropertyChanged(null);

        //Msg.ShowMessageBox("BLA", "caption", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);

    }

the "view model"

public class Stuff : INotifyPropertyChanged
{
    public Stuff()
    {
        rnd = new Random();
    }

    private Random rnd;

    public int randomNumber => rnd.Next(1, 100);

    public string randomNumStr => randomNumber.ToString()+"Text";
    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    public  virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Note the truly disgusting way that I trigger the property change. I know I should use Icommand in the 'Stuff' class but I wanted to isolate the problem quickly. In my original code, it's done properly.

halfer
  • 19,824
  • 17
  • 99
  • 186
Danw25
  • 306
  • 2
  • 13
  • `UpdateSourceTrigger=PropertyChanged` can't do anything with TextBlock.Text. it affects updates from view to view model. TextBlock.Text doesn't change from view. It is likely update of `UnderLineMsg` removes other content of the outer TextBlock – ASh Aug 15 '18 at 13:39
  • Just to make this clear: `UpdateSourceTrigger=PropertyChanged` has nothing to do with the `PropertyChanged` event of the INotifyPropertyChanged interface. You should remove it. That said, you should perhaps show us the code of your OnPropertyChanged method. – Clemens Aug 15 '18 at 13:42
  • isolated the issue, see edit – Danw25 Aug 15 '18 at 14:08
  • @Danw25 Why do you have a TextBlock within a TextBlock? – Dipen Shah Aug 15 '18 at 14:10
  • @DipenShah the original issue had a hyperlink within a textblock. the addition shows that that even without the hyperlink the nested member is removed – Danw25 Aug 15 '18 at 14:13
  • @Danw25 By setting both TextProperty and hosting a ContentElement inside a TextBlock what do you expect? Whenever first property will be used second one will not be used ever. – Dipen Shah Aug 15 '18 at 14:16
  • @Danw25 Issues is not with INotifyPropertyChanged, it is with the wa you are laying out your TextBlock. – Dipen Shah Aug 15 '18 at 14:17
  • @dipenShah this is what i was basing my original question on : https://stackoverflow.com/questions/2092890/add-hyperlink-to-textblock-wpf i need to put a hyperlink at the end of a text and i want to avoid creating a new grid for new reason (one column for the text, another for the hyperlink) See the top of the question for the original code. is the reference bad? i dont understand your response – Danw25 Aug 15 '18 at 14:26
  • Why are you binding the `Text` property *and* add a `Hyperlink` to the same `TextBlock`? This is your issue. The text "overwrites" the `Hyperlink`. – mm8 Aug 15 '18 at 14:30
  • @mm8 i need both the first textblock text and the hyperlink text binded. and the only way that i can put them in the same stackPanel and continue the same sentence structure is to put the hyperlink inside the texblock. – Danw25 Aug 15 '18 at 14:35
  • @Danw25: No. You could use a Run elmement. See my answer. – mm8 Aug 15 '18 at 14:38

1 Answers1

2

Don't bind the Text property of a TextBlock that you are also adding a Hyperlink to. Raising the PropertyChanged event for the source property will then clear out the Hyperlink.

Instead of binding the Text property of the TextBlock itself, you could add a Run element to it:

<TextBlock
    TextWrapping ="Wrap" Height="33" FontSize="12"
    Visibility="Visible"
    TextAlignment="Center"
    Foreground="#FFFFFF"
    Opacity="0.5">  
    <Run Text="{Binding UnderLineMsg, Mode=OneWay}" />
    <Hyperlink  Name="PrivacyNoticeLink2" Command="{Binding OpenPrivacyNoticeCommand}">   
        <TextBlock 
            Visibility="Visible"
            Name="privacyNoticeText2"
            Text="{Binding PrivacyNoticeButtonLabel,FallbackValue='privacy notice' ,UpdateSourceTrigger=PropertyChanged}"/>                        
    </Hyperlink> 
</TextBlock>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • a. took me a minute to notice that the textblock is white XD on my isolated solution b. works perfectly. can you please explain what is the difference between Text and ? – Danw25 Aug 15 '18 at 14:51
  • A `TextBlock` can *either* contain `Inline` elements such as `Runs` and `Hyperlinks` or a `string`. – mm8 Aug 15 '18 at 14:54