C# WPF Databinding

Databinding in General

If it doesn’t work check:

  • You can just bind to a property. Check that you are not binding on a field

Databinding for ContextMenus in General

ContextMenus in WPF are somehow special. They exist outside the visual tree of the parent control and therefore they are unable to find for example the root element by name. In the following example the binding doesn't work:

But after you use the following code in the Initialization in the Code behind file it does work:

Databinding Elements in a ComboBox

Define the class you want to show in the ComboBox:

Create a collection of the objects that you want to present in the ComboBox

Define the view where we want to show the the ComboBox

Set the correct DataContext in the views code behind:

Commands: Reevaluate CanExecute

If you change something so that a command would be enabled it is possible that WPF doesn’t reevaluate the state. It is possible however to force the system to reevalute:

Binding RadioButtons to an Enumeration

Source: See MVVM – Binding Multiple Radio Buttons To a single Enum Property in WPF or WPF radio buttons and enumeration values

Remember the following pitfall: If you enum is inside another class you need to use the + operator like

Two-way binding pitfall

If using two-way binding you need to specify a path or xpath. Therefore

will not work but

will.

NotifyCollecitonChangedAction pitfall

When listening for the NotifyCollectionChanged action of a collection one might think to catch every (regular) case when implementing the Add and Remove action. But the action Reset happens also quite frequently for example when calling Clear on a list

Call a method everytime a DependencyProperty changes

You can always listen to the general handler but sometimes the code gets more readable if a dedicated method is called for some property changes. This can be done with the following code:

Here is an example how to call it:

Read-only Dependency property

It is not enough to make the setter of a dependency property private and implementing it like regular read-only properties. The problem is that the value could be overwritten for example by animations. Here is the correct implementation:

C# Disable JavaScript error in WebBrowser control

When using the WebBrowser control and loading a html page it is possible that a message appears that there is a javascript error. This is especially the case if the webpage has been saved from the web and is then displayed on a computer which is offline. Unfortunately there is no property on the browser control which can be used to suppress this kind of error. It is however possible to inject a script tag in the header of an loaded html-file which does catch all errors. Therefore no message will be displayed. You can use the following snippet:

The injection does also work if a file like a word-document or a pdf-file is opened within the webbrowser. There won’t be an error because it is no html-file.

You might get an error (Interop type xxx cannot be embedded. Use the applicable interface instead) which can be resolved with a reference to “Microsoft HTML Object Library (MSHTML) choosing EmbedInteropTypes = False in the properties

Oracle Collection of Useful Stuff

General pitfalls

  • Every statements should be ended with ;
  • Create Procedure statements shall be ended with a ; and a / on the next line like:
  • Identifiers shall have a maximum of 30 characters, otherwise the code using them fails

Output text from within a script

Execute content of another file

Loads the content of the file script.sql and executes it.

Alter a table in a procedure

If you want to use DDL in a procedure you need to wirte it as Dynamic SQL not Static SQL.

Drop index if exists

Sometimes you don’t want an SQL-Script to raise an exception if you try to delete an index which does not exist. But unfortunately there is no if exists option for the drop index statement. You can however define a procedure for that case:

Drop materialized view if exists

Sometimes you don’t want an SQL-Script to raise an exception if you try to delete a materialized view which does not exist. But unfortunately there is no if exists option for the drop materialized view statement. You can however define a procedure for that case:

Drop view if exists

Sometimes you don’t want an SQL-Script to raise an exception if you try to delete a view which does not exist. But unfortunately there is no if exists option for the drop view statement. You can however define a procedure for that case:

Fixing ORA-29861:
domain index is marked LOADING/FAILED/UNUSABLE

First of all you need to identify the indices. This can be done with the following statement:

The easiest solution to get rid of the error is to drop and recreate those indices

Batch-Script to Startup sqlplus with a connection string

The following script asks for a username and password and does then startup sqlplus logged on with the specified user.

If you want to login as sysdba using the batchscript you need to change the sqlplus command to:

Combining the rows of multiple queries into one resultset

The above query would count all translations in the tables german, french and english

Set a new password for a user