HOW-TO: Creating a simple UserControl for displaying a FlowDocument

September 12, 2007

Hello. This is my first post about WPF.

This how-to will show you how to create a UserControl in which you can show a FlowDocument by passing a string containing the FlowDocument. It also allows Data Binding.

Lets start.

1. Create a UserControl called FlowDocumentViewer in Expression Blend (you can also do this in Visual Studio if you want) and add a FlowDocumentScrollViewer to LayoutRoot. Name it flowDocument (you can name it like you want, but I will be using this name in this guide). Your structure should be as follows:

Structure

2. Now, open the project in Visual Studio and add the following code to your FlowDocumentViewer.xaml.cs:

        public static readonly DependencyProperty ContentFlowDocumentStringProperty = DependencyProperty.Register(
            "ContentFlowDocumentString",
            typeof(string),
            typeof(FlowDocumentViewer),
            new FrameworkPropertyMetadata(null,
                FrameworkPropertyMetadataOptions.AffectsRender,
                new PropertyChangedCallback(DescriptionFlowDocumentUI.OnContentChanged)
            )
        );

        private static void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            FlowDocumentViewer control = (FlowDocumentViewer)d;
            control.flowDocument.Document =
                (System.Windows.Documents.FlowDocument)System.Windows.Markup.XamlReader.Load(
                    new MemoryStream(System.Text.UTF8Encoding.Default.GetBytes((string)e.NewValue))
                );
        }

        public string ContentFlowDocumentString
        {
            get { return (string)GetValue(ContentFlowDocumentStringProperty); }
            set { SetValue(ContentFlowDocumentStringProperty, value); }
        }

Lets see what this code does.

a) We added a DependencyProperty called ContentFlowDocumentStringProperty to our class. This DependencyProperty “points” at ContentFlowDocumentString property, as well as it tells, that this property of type string and we registered a callback function (OnContentChanged), which will be triggered when ContentFlowDocumentString is changed. Note that FrameworkPropertyMetadataOptions.AffectsRender piece of code. This tells WPF, that when this property is changed it affects the UI looks, so that WPF knows he has to render something.

b) The OnContentChanged function is called whenever our DependencyProperty is changed. Here we do the actual work of changing the FlowDocument in our control. We get the instance as an argument (DependencyObject d, which we cast to FlowDocumentViewer explicilty) and we create a new FlowDocument from the newly assigned string ((string)e.newValue).

c) Finally we added the actual property – ContentFlowDocumentString. See those GetValue and SetValue? Don’t be surprized, it is how DependencyProperties work. You don’t have a special (private) field in your class for them. WPF does all the work for you.

Well, that is pretty much it.

The usage is simple:

FlowDocumentViewer fdv = new FlowDocumentViewer();
fdv.ContentFlowDocumentString = stringContainingFlowDocumentXAML;

Or if you want to use data binding, it is absolutely similar to casual binding in WPF:

Binding b = new Binding();
b.Source = myBindingSource;
b.Path = new PropertyPath("PathToProperty");
FlowDocumentViewer fdv = new FlowDocumentViewer();
fdv.SetBinding(FlowDocumentViewer.ContentFlowDocumentStringProperty, b);

If you have questions / remarks / etc. feel free to post them. I will try to answer.


eDonkey

August 8, 2007

зашёл сегодня на сайт еДанки. и прямо офигел. мало того, что я не знал что всё накрылось медным тазом, так они ещё и “страшный” текст туда написали. Ужас какой. Кому лень туда идти, то вот:

If you steal music or movies, you are breaking the law.

Courts around the world — including the United States Supreme Court –
have ruled that businesses and individuals can be prosecuted for illegal
downloading.

You are not anonymous when you illegally download copyrighted material.

Your IP address is xxx.xxx.xxx.xxx and has been logged.

Respect the music, download legally.

О ужас! Мой ИП залогили! Что мне делать?! Ааааааааааа! Ко мне же придут! Заберут в американский супер-суд. Прощайте, дамы и господа!


YouTube – 500 Internal Server Error. A nice joke from YouTube.

July 28, 2007

So today I hit an internal server error @YouTube. It said that it would be nice of me to report the thing. “Well”, – I though, – “I could report something once in a lifetime”. I decided to report. The error page had a nice link to the report form. Clicked it, filled it in. The error page asked me to include some kind of encoded abra-cadabra in my report (example of this arba-cadabra: “0rF3m6tOvt-q5Dlktd3-fpLj8oGDzySl21207UB … … … Rr7B1cjENm_TJhEY5_cagqyxHYMPi9HjU3o=”). Ok, no problemo! Copy-paste into the “Message” box. Nothing else to do – I press “Submit”. Well-well-well, what have we got here? An error! Here it is:

Please enter less than 350 characters (currently 2044 characters)

Wow! They ask me to submit some debug info and after that tell, that it is too long. Sheesh. That was stu-pid. Way to go, YouTube!


HOW-TO: Linksys WUSB54GC Wireless and Ubuntu Linux (7.04 Feisty / 7.10)

July 24, 2007

Drivers for WUSB54GC

Hi, this is a guide I decided to write after buying and installing a Linksys WUSB54GC wireless USB adapter. The guide is very simple.

What you need to get this device going is to install ndiswrapper and the driver. You can use the driver attached to this post (or you can go to Linksys official web-site, download the driver (not the Vista one), unpack it (I know that it is an EXE file, but you can still unpack it) and find the drivers in the Drivers folder – that’s exactly how I got the drivers I attached to this post).

Ok, now the installation procedure. Lets assume you unpacked the drivers into your home directory (~) – the should be three files:

  • rt73.cat
  • rt73.inf
  • rt73.sys

1. Install ndiswrapper:

apt-get install ndiswrapper

2. Change the dir to where your driver files are located:

cd ~

3. Install the drivers:

ndiswrapper -i rt73.inf

ndiswrapper -m

ndiswrapper -mi

ndiswrapper -ma

4. Reboot. Now the device should start working – you can verify that by clicking on the nmapplet or by going to Administration->Network – it should be there and it should show you wireless networks. I got it working that way and it correctly shows the signal strength, connects nicely – works nicely.

Now, why I used ndiswrapper – before I installed and configured ndiswrapper the device was also present and it even showed me the wireless networks, but it did not connect to any network.


Linux Application Developement

May 25, 2007

God! I really love Linux (especially Ubuntu), I really do. But! Please, someone tell me how the hell is anyone supposed to develop GUI apps here? I know, I know. There are tons of development tools. Glade, MonoDevelop, Anjuta etc. But! I tried them. The only friendly tool was MonoDevelop. But really, I created a Gtk# program (project to be exact) and just screamed! Why you ask? Because I tried to crate a button. It created. No problem. Then I thought that it would be nicer if the button had a pretty background image. Know what? IT IS NOT POSSIBLE TO SET A BACKGROUND!!! That sucks big time. I do not need this ugly button with no-good design. Oh, oh. And one more thing. I certainly tried to compile the thing and it DIDN’T work. Because the assembly was not found. Now what the hell is that?.. EVERY dev tool in Linux I saw in NOTHING compared to MSVS (which I use all the time). Nothing I tell you. And don’t try telling me stories about real programmers who can make everything possible. I don’t need the freaking routine! I need a good IDE where I can forget about details. I need normal control out-of-the-box. With options. With yummy features and stuff. Look at .NET! You call Mono a .NET analogue? B-u-l-l-s-h-i-t. Mono is a zillion kilometers away from .NET. Not even a bit close. Well, I know, it’s not version 1.0 at all. But what the point PR-ing it so much? Almost full System.Windows.Forms? Where? Oh, oh, MonoDevelop has a VS2005 project import. Yeah. Sure. It imports. With a detail that nothing works. Edit my GUI code in source view? Do I look like crazy? No way I’m going to do that.

Now, the point is, linux REALLY needs something like MSVS. MonoDevelop is, in my opinion, the closest analog, but it is still far-far away from it. No….. MonoDevelop is even ok, compared to framework.

Oh. One more thing. If you are thinking about writing an angry post about not respecting someone’s work – that’s not right. I think that devs are doing a great job. I really do. I complain about the whole situation. I will never switch to Linux development I guess. I always try to support people doing real things in this world, but in this case I just have to admit – Microsoft is doing a far better job than the Linux world.


Zend Studio 5.5 – Antialiasing the font (tested on Ubuntu 7.04

May 18, 2007

Hi! Today I am very happy to announce that I am now running Ubuntu 7.04!

Now, no long preludes, since this post is about “How the <fluffy bunny> do I get those antialiased fonts to word in Zend Studio?!”. That was the question for me about 5 minutes ago. Well, I really thought that Zend was perfect – usually, if they have this checkbox – it works without tweaking. Now here’s the disappointment part – that Tools -> Preferences -> Desktop -> Use Antialised Fonts didn’t really work. I asked my best friend (his name’s Google) -oh, whyyyy?… Google was silent… he didn’t know. Fortunately, Google remembered that Zend has a forum! Yay! Searched the forum and found the answer.

Here goes:

… in <installdir>/bin, edit ZDE.lax adding the following line at the end

lax.nl.java.option.additional=-Dswing.aatext=true

restart ZDE and you should have antialiased fonts… ” (c) pkoebbe

That worked like magic. No problems at all! So, I hope that this helps someone and that my friend Google will soon cache this post.

PS The original post on Zend forums is here.


My ICQ number changed

March 4, 2007

I officially declare that my old ICQ was stolen (175-685-828), so if you are communicating with me with that number it means that you are talking to some pervert! My new ICQ is 461-181-725 – please update your contact list.


A bug @ www.hot.ee

February 27, 2007

I cleaned out my email account on www.hot.ee today and encountered a very interesting bug. Well, see for yourself:Hot.ee on fire

Nice, eh? Lugemata (unread): 0 + 7 + 0 + 0 = 577! Nice math. Love it.

Maht (size): 36 + 312 + 0 + 241 + 0 = 10608Kb! Wow! Now I can’t sleep at night.


MySQL Connector/Net 1.0.8 & 5.0.3

February 27, 2007

Ok, so here I am working with MySQL Connector/NET. I have the latest version of it (1.0.8). Everything worked properly in 1.0.7, so I thought that the updated version should work properly too. Well, no, actually. I don’t know what are the devs thinking, but I am outraged.

The problem is that I download a mysql dump file and after that using MySQL Connector update the local database. You think it is easy, right? Well, that’s what I thought in the first place, but turns out that it just doesn’t work.If you know C# & .NET then you know that dumps should be inserted with ExecuteNonQuery() function. Ok, sounds easy, right? Worng, all the time this crappy driver threw an exception saying “Query was empty”. Wtf? I even created a breakpoint and inserted an MessageBox.Show(command.CommandText). The query was there. I spent hours on searching the internet about this bug (or feature?). I found one bug report saying, that carriage returns were causing this and that it was fixed in 1.0.7. Screw that, this bug IS PRESENT in 1.0.8 AND 5.0.3. How do I know? Well, because I replaced all returns with spaces and it worked out perfectly! Now why the fuck I should wark around bugs, that were already fixed?? Oh, oh, and by the way – I can not post a bug report either, because on the mysql bug reporting page it says, that “cannot connect to MySQL”. That is freaking awesome – mysql’s bug reporting tool is not working because their mysql is down! In-fucking-credible!!

So, for all you guys in the same situation – you MUST remove all carriage returns in your queried before doing an ExecuteNonQuery()! Otherwise you WILL get an exception.

And for MySQL Connector/NET devs – do something about it ASAP. And by the way – this bug is also present in MySQL Connector/NET 5.0.3 (not only in 1.0.8).


The best linux FTP client: FileZilla

February 12, 2007

Hello, today I want to tell you about my search for a nice FTP client. First things first – I’ve been using gFTP for a long time and I find it to be nice, but to say the truth it lacks some features and is quite buggy. There is no recursive chmod, it crashes every time when internet stops working (this is important, since I use WiFi all the time), it waits 30 seconds if he got disconnected from server (and I haven’t found a way to stop this except restarting gFTP). That is not all, but I think that you get the point :)

And this is the best FTP client I knew for my Ubuntu Edgy until now. Today I found out that the FileZilla FTP client is being made cross-platform. I use that one all the time in Windows and let me put it short – “IT ROCKS!!!”. No, really, it has a very nice site manager, it does not crash (not once since I installed it on Windows and I’ll need some time to see how it goes under linux), has a great user interface, transfer queue and so on. Also it allows you to install themes (icons for buttons) ^_^

Overall, I find FileZilla to be the best choise for Linux. It is still in beta, but it is A LOT more stable that gFTP. So, if you have Ubuntu Edgy installed and you’re wiling to try it out – “apt-get install filezilla”! Don’t know about others, but Edgy repositories have it.

And in the end I’ll post an overview and features from FileZilla official site:

Overview

FileZilla is a fast and reliable FTP client for windows with lots of useful features and an intuitive interface.

Features

Among others, the features of FileZilla include the following:

  • Easy to use
  • Multilingual (English, German, French, Japanese, just to name a few)
  • Strong encryption support using SFTP over SSH and FTP over SSL/TLS
  • Supports transfer resuming and files larger than 4 GiB
  • Site Manager
  • Queue support
  • Proxy support
  • Speed limits
  • MODE Z transfer compressing
  • … and more!