Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

Paragraph.Text? Should there be a dependency property as well?

Closed Thread
 
Thread Tools Display Modes
Old 01-10-2006   #1 (permalink)
Jason Dolinger
Guest


 

Paragraph.Text? Should there be a dependency property as well?

I'd like to set up databinding to a Paragraph object. Paragraph has a
Text field, and I would like bind this to a particular cell in a
DataTable. I thought that one would do this using some code like the
following:

Paragraph p = new Paragraph();
Binding b = new Binding("Table.Rows[" + rowCount + "][" + columnCount
+ "]");
b.Mode = BindingMode.TwoWay;
b.Source = this.DataView;
p.SetBinding(Paragraph.TextProperty, b); // does not compile!

This doesn't compile because Paragraph does not have a static dependency
property for it's Text property in a similar way that a TextBlock does.
Was this purely an oversight and it will be incorporated in a future
release, or is this by design?

Regards,
Jason
Old 01-10-2006   #2 (permalink)
loser
Guest


 

Re: Paragraph.Text? Should there be a dependency property as well?

I believe that's intentional, I have no clue why, but it is. The
solution is to derive from Paragraph and make a DependencyProperty for
it.

And to Avalon folks- this is what makes templates completely worthless.
Paragraph, Hyperlink, etc are all unuseable in templates. Do you guys
actually use this stuff before you send it out? When you made the
*text* property on paragraph and hyperlink *unuseable* from templates
what were you thinking???

Old 01-10-2006   #3 (permalink)
loser
Guest


 

Re: Paragraph.Text? Should there be a dependency property as well?

Sorry for my previous rant, it's just that when I hear all this hype
about templates and styling, then can't use a simple hyperlink in one,
it makes me think microsoft is full of ****. I drank the koolaid and
saw productivity plummet trying to work around crap like this.

Old 01-10-2006   #4 (permalink)
viliescu
Guest


 

RE: Paragraph.Text? Should there be a dependency property as well?

I had this problem before with Hyperlink, so I have put a TextBlock inside
the Hyperlink and use its Text property for data binding. I presume it will
work for Paragraph too.

Regards,
Valentin

"Jason Dolinger" wrote:

> I'd like to set up databinding to a Paragraph object. Paragraph has a
> Text field, and I would like bind this to a particular cell in a
> DataTable. I thought that one would do this using some code like the
> following:
>
> Paragraph p = new Paragraph();
> Binding b = new Binding("Table.Rows[" + rowCount + "][" + columnCount
> + "]");
> b.Mode = BindingMode.TwoWay;
> b.Source = this.DataView;
> p.SetBinding(Paragraph.TextProperty, b); // does not compile!
>
> This doesn't compile because Paragraph does not have a static dependency
> property for it's Text property in a similar way that a TextBlock does.
> Was this purely an oversight and it will be incorporated in a future
> release, or is this by design?
>
> Regards,
> Jason
>

Old 01-10-2006   #5 (permalink)
Drew Marsh
Guest


 

Re: Paragraph.Text? Should there be a dependency property as well?

loser wrote:

> Sorry for my previous rant, it's just that when I hear all this hype
> about templates and styling, then can't use a simple hyperlink in one,
> it makes me think microsoft is full of ****. I drank the koolaid and
> saw productivity plummet trying to work around crap like this.


I think you need to take a step back and realize this is a "work-in-progress"
code base your playing with and the only reason it's in the public's hands
at all is so that we can provide constructive feedback on things like this.
Maybe if you shared your templating scenario with the group or entered it
into LadyBug Microsoft would get a chance to understand your scenario and
see that maybe these features do need to be implemented.

Later,
Drew


Old 01-10-2006   #6 (permalink)
Jason Dolinger
Guest


 

Re: Paragraph.Text? Should there be a dependency property as well?

loser wrote:
> I believe that's intentional, I have no clue why, but it is. The
> solution is to derive from Paragraph and make a DependencyProperty for
> it.
>
> And to Avalon folks- this is what makes templates completely worthless.
> Paragraph, Hyperlink, etc are all unuseable in templates. Do you guys
> actually use this stuff before you send it out? When you made the
> *text* property on paragraph and hyperlink *unuseable* from templates
> what were you thinking???
>


Ok, so let's say I do something like that:


public class BindableCell : Paragraph
{

public static readonly DependencyProperty TextProperty = null;


static DataGridCell() {
TextProperty = DependencyProperty.Register(
"Text", typeof(string), typeof(BindableCell ), new
PropertyMetadata(1));
}


}

How exactly does this dependency property get tied to the existing
"Text" property inherited from the Paragraph?

Thanks for the help!
Jason
Old 01-10-2006   #7 (permalink)
Jason Dolinger
Guest


 

Re: Paragraph.Text? Should there be a dependency property as well?

Jason Dolinger wrote:
> loser wrote:
>
>> I believe that's intentional, I have no clue why, but it is. The
>> solution is to derive from Paragraph and make a DependencyProperty for
>> it.
>>
>> And to Avalon folks- this is what makes templates completely worthless.
>> Paragraph, Hyperlink, etc are all unuseable in templates. Do you guys
>> actually use this stuff before you send it out? When you made the
>> *text* property on paragraph and hyperlink *unuseable* from templates
>> what were you thinking???
>>

>
> Ok, so let's say I do something like that:
>
>
> public class BindableCell : Paragraph
> {
>
> public static readonly DependencyProperty TextProperty = null;
>
>
> static BindableCell() {
> TextProperty = DependencyProperty.Register(
> "Text", typeof(string),
> typeof(BindableCell ), new PropertyMetadata(1));
> }
>
>
> }
>
> How exactly does this dependency property get tied to the existing
> "Text" property inherited from the Paragraph?
>
> Thanks for the help!
> Jason


Ah hah! This does is:


public class BindableCell : Paragraph
{

public static readonly DependencyProperty BlahProperty = null;


static BindableCell() {
TextProperty = DependencyProperty.Register(
"Text", typeof(string),
typeof(BindableCell), new PropertyMetadata(new
PropertyChangedCallback(TextUpdated)));
}

private static void TextUpdated(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{

BindableCell cell = d as BindableCell;
cell.Text = (string) e.NewValue;

}
}

Is this the best approach?

Jason
Old 01-10-2006   #8 (permalink)
Drew Marsh
Guest


 

Re: Paragraph.Text? Should there be a dependency property as well?

Jason Dolinger wrote:

> Ok, so let's say I do something like that:
>
> ... code snipped for brevity's sake ...
>
> How exactly does this dependency property get tied to the existing
> "Text" property inherited from the Paragraph?


Before I even answer the question, I'd just like to give a little insight
into why I think Paragraph's Text property isn't a DependencyProperty right
now. The truth is, it's not a property at all. Even though it's presented
as a CLR property, what's happening under the covers is that the Paragraph
class is using the TextPointer API to grab it's content out of (or replace
the content in) the TextRange it belongs to. It's nice of the Paragraph class
to abstract away the TextRange API like this, in fact I'm sure more people
would be kicking and screaming if it weren't designed as a CLR property because
then they'd have to do all the TextRange work themselves. That said, it *could*
still be a DP AFAICT, but maybe Microsoft has a good reason not to make it
one that I'm missing.

Now, as to how to "turn it into" a DP with your own subclass...

When registering the DP, you need to pass an instance of FrameworkPropertyMetaData.
You should probably look at configuring the metadata correctly so it supports
binding, doesn't support animation, etc., but the most import thing is to
set the Get/SetValueOverride properties to delegate instances that you then
use to access the base class' Text property from. Something like:

private static object MyGetValueOverride(DependencyObject do)
{
return ((Paragraph)do).Text;
}

private static void MySetValueOverride(DependencyObject do, object value)
{
if(value == null)
{
throw new ArgumentNullException("value");
}

string text = value as string;

if(text == null)
{
throw new ArgumentException("Expected a string.");
}

((Paragraph)do).Text = text;
}

HTH,
Drew


Old 01-10-2006   #9 (permalink)
Nick Kramer [MSFT]
Guest


 

Re: Paragraph.Text? Should there be a dependency property as well?

> I'd just like to give a little insight into why I think Paragraph's Text
> property isn't a DependencyProperty right now. The truth is, it's not a
> property at all. Even though it's presented as a CLR property, what's
> happening under the covers is that the Paragraph class is using the
> TextPointer API to grab it's content out of (or replace the content in)
> the TextRange it belongs to.


Rich text is a lot more complicated than plain text, which is almost
certainly part of the story. But we're not entirely consistent here,
TextBox.Text and TextBlock.Text are data bindable DP's, but Paragraph.Text &
Span.Text is not. (that's plain text binding -- rich text data binding is
complicated enough that we don't plan to do that in v1) We'll investigate
this and see if we can add data binding to those .Text properties, thanks
for bringing this to our attention.

-Nick Kramer [MSFT]
http://blogs.msdn.com/nickkramer

---
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"Drew Marsh" <drub0y@hotmail.no.spamming.com> wrote in message
news:f01844f1e22348c7df7737205694@msnews.microsoft.com...
> Jason Dolinger wrote:
>
>> Ok, so let's say I do something like that:
>>
>> ... code snipped for brevity's sake ...
>> How exactly does this dependency property get tied to the existing
>> "Text" property inherited from the Paragraph?

>
> Before I even answer the question, I'd just like to give a little insight
> into why I think Paragraph's Text property isn't a DependencyProperty
> right now. The truth is, it's not a property at all. Even though it's
> presented as a CLR property, what's happening under the covers is that the
> Paragraph class is using the TextPointer API to grab it's content out of
> (or replace the content in) the TextRange it belongs to. It's nice of the
> Paragraph class to abstract away the TextRange API like this, in fact I'm
> sure more people would be kicking and screaming if it weren't designed as
> a CLR property because then they'd have to do all the TextRange work
> themselves. That said, it *could* still be a DP AFAICT, but maybe
> Microsoft has a good reason not to make it one that I'm missing.
>
> Now, as to how to "turn it into" a DP with your own subclass...
>
> When registering the DP, you need to pass an instance of
> FrameworkPropertyMetaData. You should probably look at configuring the
> metadata correctly so it supports binding, doesn't support animation,
> etc., but the most import thing is to set the Get/SetValueOverride
> properties to delegate instances that you then use to access the base
> class' Text property from. Something like:
>
> private static object MyGetValueOverride(DependencyObject do)
> {
> return ((Paragraph)do).Text;
> }
>
> private static void MySetValueOverride(DependencyObject do, object value)
> {
> if(value == null)
> {
> throw new ArgumentNullException("value");
> }
>
> string text = value as string;
>
> if(text == null)
> {
> throw new ArgumentException("Expected a string.");
> }
>
> ((Paragraph)do).Text = text;
> }
>
> HTH,
> Drew
>
>



Old 01-10-2006   #10 (permalink)
Drew Marsh
Guest


 

Re: Paragraph.Text? Should there be a dependency property as well?

Nick Kramer wrote:

> Rich text is a lot more complicated than plain text, which is almost
> certainly part of the story. But we're not entirely consistent here,
> TextBox.Text and TextBlock.Text are data bindable DP's, but
> Paragraph.Text & Span.Text is not. (that's plain text binding -- rich
> text data binding is complicated enough that we don't plan to do that
> in v1) We'll investigate this and see if we can add data binding to
> those .Text properties, thanks for bringing this to our attention.


Looking at how TextBlock does it, my guess is that they left it out of these
document specific elements because it would add soooo much overhead to complex
documents. Every Paragraph/Span would not only store it's text in the DP
cache (for all the benefits of DPs), but also in the TextContainer they're
associated with. So if you had a 10 page document with 100 some odd Paragraphs
you'd have 100 strings of varying sizes stored in the DP engine as well as
the same amount of string data stored in the underlying TextContainer.

Also, wouldn't you need to hook the Change event of the underlying TextRange
of the Paragraph/Span? Just thinking of the scenario where someone comes
in through the TextRange API and modifies part of the Paragraph/Span. I would
think the expected thing would be for the DP value to be updated. This adds
all the overhead of hooking an event handler to every Paragraph/Span. I don't
actually see that being done anywhere in TextBlock, maybe that's an expected/acceptable
limitation though?

Anyway, that's just my best guess at why they made the sacrafice... you would
certainly know better than I.

Cheers,
Drew


Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
WPF: Autogenerate a Private Dependency Property ? Jules Winfield .NET General 4 03-06-2008 09:43 AM
Binding to a custom dependency property diffeq Avalon 1 07-02-2007 01:42 PM
Trigger on user dependency property Griff Avalon 1 01-31-2006 06:59 AM
Dependency property not initialized in OnPropertyChanged? Jared Bienz Avalon 0 01-10-2006 03:53 PM
Custom Dependency Property and Binding CSkinner Avalon 3 01-10-2006 03:53 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50