3

I'd like an advice to the following problem: I want to embed a Button into a text flow, but when I embed a Button and Label (or TextBlock) into the WrapPanel, I get the first figure:

alt text http://sklad.tomaskafka.com/files/wpf-wrappanel-problem.png

I think that one of solutions could be FlowDocument, but I feel that this is far too heavy for a control simple like this (which could be used in several hundred instances). Do you have some other ideas about how to implement this? Thank you!

EDIT: One solution could be the following (I didn't know it was possible to put more stuff into TextBlock), but I would lose the ability to bind (which I need):

<TextBlock TextWrapping="Wrap">
    <Span>
        <Button x:Name="MyButton" Command="{Binding Path=MyCommand}" Content="+" />
        <Run x:Name="MyLabel" Text="{Binding Path=Subject}" />
        <!--
        Problem: binding makes following error:
        A 'Binding' cannot be set on the 'Text' property of type 'Run'. 
        A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
        -->
    </Span>
</TextBlock>
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Tomáš Kafka
  • 4,405
  • 6
  • 39
  • 52

4 Answers4

1

To bind to Run.Text, checkout the BindableRun class by Fortes. Simple to implement, I use it all over my projects.

PortageMonkey
  • 2,675
  • 1
  • 27
  • 32
  • LOL, I found this 5 minutes ago and just implemented a solution with that :)). Thanks anyway :). – Tomáš Kafka Aug 16 '09 at 00:46
  • I found that implementing BindableRun correctly is pretty tricky - yours (and almost all other available implementations) will cause an exception from wpf layouting engine when the bound content changes from null to something non-null. Corrrect implementation from Microsoft is here - but really tricky http://msdn.microsoft.com/en-us/magazine/dd569761.aspx – Tomáš Kafka Nov 30 '09 at 00:44
1

I found that implementing BindableRun correctly is pretty tricky - and almost all other available implementations will cause an exception from wpf layouting engine when the bound content changes from null to something non-null - see this problem, keyword "Collection was modified; enumeration operation may not execute."

Corrrect implementation from Microsoft is here - it shows how tricky this really is.

Tomáš Kafka
  • 4,405
  • 6
  • 39
  • 52
0

Solution: BindableRun class + the following markup:

<TextBlock>
    <Button x:Name="MyButton" Command="{Binding Path=MyCommand}" Content="+" />
    <common:BindableRun x:Name="Subject" BindableText="{Binding Path=Subject}"/>
</TextBlock>
Tomáš Kafka
  • 4,405
  • 6
  • 39
  • 52
0

Funny thing it works on the designer of a UserControl... In that case, using the Property Change of your control to set the value to the Run is enough. I mean, if you had something like:

<TextBlock>          
   <Run Text="{Binding ElementName=thisCtrl, Path=Description}" />
</TextBlock>

Then just name the run, and on your property change handler of your UserControl DependencyProperty get/set the value.

Guillermo Ruffino
  • 2,940
  • 1
  • 26
  • 23