RSS Feed


Commands in WPF
Posted by Alan Le | 4 Comments | Trackback Url | Bookmark with:        
Tags:

Links to this Post

Comments

Thursday, 31 May 2007 04:08 by Re: Commands in WPF
<div class=ExternalClassC2F4B83112994F39AB38C7C5A3390D4E>Hi Alan, I've just looked at your sample. It is clear and easy how commands work in this sample but in a real life scenario the bold button for example should reflect if the current selected text is bold or not. The same applies to italic and the paragraph alignment. Is there any way of achieving this with commands? I wonder how useful Commands really are if there isn't a way of reporting back a custom state other than CanExecute. I've asked this question on the WPF Forum as well: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1670825&amp;SiteID=1</div>

Thursday, 31 May 2007 04:39 by Re: Commands in WPF
<div class=ExternalClassCEDC49FDF7544DC7817458E7256CEA31><div>Hi Patrick, thanks for the comment. Interesting point about the button state based on selected text in the richtextbox.  I don't think there is built-in mechanism with the Command Pattern to support that.</div> <div> </div> <div>I think there's a way though.  As a proof of concept, i wrapped my bold button control inside of a checkbox control so i can set whether it's checked or not.</div> <div> </div> <div><font color="#0000ff" size=2> <p>&lt;</font><font color="#a31515" size=2>CheckBox</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>x:Name</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>BoldButton</font><font size=2>&quot;</font><font color="#0000ff" size=2>&gt;</p> <p>&lt;</font><font color="#a31515" size=2>Button</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Style</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>{StaticResource formatTextStyle}</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Command</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>EditingCommands.ToggleBold</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>ToolTip</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>Bold</font><font size=2>&quot;</font><font color="#0000ff" size=2>&gt;</p> <p>&lt;</font><font color="#a31515" size=2>TextBlock</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>FontWeight</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>Bold</font><font size=2>&quot;</font><font color="#0000ff" size=2>&gt;</font><font size=2>B</font><font color="#0000ff" size=2>&lt;/</font><font color="#a31515" size=2>TextBlock</font><font color="#0000ff" size=2>&gt;</p> <p>&lt;/</font><font color="#a31515" size=2>Button</font><font color="#0000ff" size=2>&gt;</p> <p>&lt;/</font><font color="#a31515" size=2>CheckBox</font><font color="#0000ff" size=2>&gt;</p></font></div> <div>Then to set the IsChecked property I have a handler on the richTextBox's SelectionChanged event.</div> <div> </div> <div><font size=2> <p></font><font color="#0000ff" size=2>void</font><font size=2> mainRTB_SelectionChanged(</font><font color="#0000ff" size=2>object</font><font size=2> sender, </font><font color="#2b91af" size=2>RoutedEventArgs</font><font size=2> e)</p> <p>{</p> <p></font><font color="#0000ff" size=2>if</font><font size=2> (!mainRTB.Selection.GetPropertyValue(FontWeightProperty).Equals(</font><font color="#2b91af" size=2>DependencyProperty</font><font size=2>.UnsetValue))</p> <p>BoldButton.IsChecked = ((</font><font color="#2b91af" size=2>FontWeight</font><font size=2>)(mainRTB.Selection.GetPropertyValue(FontWeightProperty))) == </font><font color="#2b91af" size=2>FontWeights</font><font size=2>.Bold ? </font><font color="#0000ff" size=2>true</font><font size=2> : </font><font color="#0000ff" size=2>false</font><font size=2>; </p> <p>}</p> <p></font>I think this might even be done with binding in xaml and a generic dependency property to bold converter.</p></div></div>

Sunday, 3 Jun 2007 06:38 by Re: Commands in WPF
<div class=ExternalClass36B3A856EBC5487BA1D8818726E201F1>Thanks for the response Alan. I thought about something like this at first too, but that would mean that we have to 'couple' the richtext box with the button thus eliminating one of the advantages of Commands. I wonder if there is a way of transporting this information via an extended Command and a custom implementations of the command bindings. At the moment I don't have enough knowledge about Commands to come up with a proof of concept but I am eager to find out :-). If you have any hints or further ideas I would appreciate that. Cheers, Patrick</div>

Monday, 2 Jul 2007 08:22 by Re: Commands in WPF
<div class=ExternalClassE3438B59BD2B480BA670FC5E78576A1B>Hi Alan, Finally I have come up with a simple work around for this kind of problem. I've described it here: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1809894&amp;SiteID=1 cheers, Patrick</div>

Name:
URL:
Email:
Comments:

CAPTCHA Image Validation