Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to set Binding to a Run that resides in a TextBlock

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="This is my text">
  <TextBlock>
    <Run Text="{Binding}"/>
  </TextBlock>
</Window>

Throws InvalidOperationException: "Two-way binding requires Path or XPath."

Specifying Mode=OneWay, leads to a weird compiler error:

The tag 'Binding,' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.

Is there any xamly way to fix this?

like image 818
Shimmy Weitzhandler Avatar asked Jan 02 '26 08:01

Shimmy Weitzhandler


1 Answers

I have not found a reason why, but this is how you can do it without it getting too awkward:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="This is my text">
  <TextBlock>
    <Run Text="{Binding Path=.}"/>
  </TextBlock>
</Window>

For some reason

<Run Text="{Binding}" />

causes the runtime error, but

<Run Text="{Binding Path=.}" />

does not. The reason might have something to do with when you are "ambiguous" with your bindings, there are certain fallback behaviors that take over to interpret your binding. Or perhaps, this is a genuine MS Bug with interpreting {Binding} on the Run control.

like image 52
tyriker Avatar answered Jan 05 '26 15:01

tyriker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!